Translation

Translation

Startkladde uses Qt's i18n mechanism, augmented by some custom rules.

Strings

Qt's translations mechanism works by wrapping strings which should be translated in tr().

The script script/find_missing_tr can be used to find strings where the tr() invocation is missing. To make this possible, we must be able to distinguish strings that should not be translated from strings that should be translated, but the tr() invocation is missing. Therefore, strings which should not be translated must be wrapped in notr(). This macro, defined in src/i18n/notr.h, is a no-op which just expands to its argument. It has no impact on program behavior or performance.

All strings, with the exception of empty strings, should be wrapped in one of the following functions (or macros):

  • tr () - translated strings defined in QObject classes
  • qApp->translate () - translated strings defined in non-QObject classes
  • notr () - non-translated strings; expands to its argument
  • qnotr () - non-translated QStrings; shortcut for QString (notr ())
  • qnotrUtf8 () - non-translated QStrings containing UTF8 encoded characters; shortcut for QString::fromUtf8 (notr ())
  • QT_TRANSLATE_NOOP - for assigning to const char * - see below

Some files are exempt from this, i. e. strings in these files do not have to be wrapped

  • src/migrations/*.cpp

Furthermore, the arguments for the following functions or constructors do not have to be wrapped:

  • QDir ()

Sometimes, it is necessary to store a string or to pass it to a different class. An example is the title of the (generic) statistics window which is determined in MainWindow and passed to StatisticsWindow for display. If we were to translate the string in MainWindow and pass the translation result, we could not retranslate the string on language change. Therefore, we have to store the original string. The following rules apply in this case:

  • store the string in a const char * variable
  • prefix the variable name with ntr_ (for "not translated" - to make it clear that this string is not to be used directly)
  • wrap the string in QT_TRANSLATE_NOOP (this marks the string for extraction by lupdate)
  • when using the variable, pass it through tr() or qApp->translate() (this performs the actual translation)

Updating translations

When strings have been added to the source, use make lupdate to update the translation source (*.ts) files located in translations/. Update the translation source files with Qt linguist.

The Qt Linguist manual


Related

Wiki: Home