Skip to content

planed-es/QI18n

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QI18n

QI18n is an internationalization library for the Qt framework, using JSON files to load your translations.

How to use

Initialization

QI18n must be initialized before it is used. You may instantiate it in your main function, to make it available from all your application:

#include <QApplication>
#include <qi18n.h>

int main(int argc, char** argv)
{
  QApplication application(argc, argv);
  QI18n i18n(":/locales", "en"); 

  return application.exec();
}

The QI18n constructor takes at least two parameters:

  • A root path in which your JSON translations files will be looked up for
  • A default locale, which will be pre-loaded and used until you explicitely set another locale

In this example, we use ":/locales" as the root path, meaning translation files will be looked up using the Qt Resource System, in the locales folder. As for the default locale, it is set to "en", meaning translations will be loaded from all the JSON files ending with the en.json pattern.

Translating text

Translations are defined in JSON files presented as such:

{
  "hello": "Hello world !"
}

You can then access the translation from your code using:

QI18n* i18n = QI18n::get();

qDebug() << i18n->t("hello");

Categories

You can also use JSON sub-objects to sort your translations:

{
  "category": {
    "message": "Hello category !"
  }
}

Which is then used like this:

i18n->t("category.message");

Extrapolation

Extrapolation can be defined in your translations using the {{}} delimiters, such as:

{
  "hello": "Hello {{name}} !"
}

The values are then provided using a QVariantMap:

i18n->t("hello", QVariantMap{{"name", "John"}});