Wiki Table of Contents

MX translations developer

Here is some basic information for making an MX Linux QT, shell, or Python app ready to display translations of embedded English text.

Internationalizing QT apps

First, in your main.cpp, include the following statements, substituting the application’s name as appropriate in “yourappname_” and “/usr/share/yourappname/locale”:

 #include <unistd.h>
 #include <QApplication>
 #include <QTranslator>
 #include <QLocale>

 ...

 int main(int argc, char *argv[])
 {

 ...

      QTranslator qtTran;
      qtTran.load(QString("qt_") + QLocale::system().name());
      a.installTranslator(&qtTran);

      QTranslator appTran;
      appTran.load(QString("yourappname_") + QLocale::system().name(), "/usr/share/yourappname/locale");
      a.installTranslator(&appTran);

Amongst other things, this says that the individual language files containing translations will be found in /usr/share/yourappname/locale. For example, the French translation file would be:

 /usr/share/yourappname/locale/yourappname_fr.qm

where .qm is the extension used for the “lightweight” object translation lookup files for QT apps.

Second, in your application’s source file, use tr() for all literal text that you wish to appear in the language of the user’s locale, eg:

 this->setWindowTitle(tr("MX Select Sound"));

Where necessary, use replaceable arguments to allow translators to re-order text according to the syntax of their particular language:

 QString msg = tr("Free space on %1 is %2");

Third, in your qmake project file, specify the languages for which you have translations. For example:

 TRANSLATIONS += translations/yourappname_ca.ts \
            translations/yourappname_cs.ts \
            translations/yourappname_de.ts \
            translations/yourappname_el.ts \
            translations/yourappname_es.ts \
            translations/yourappname_fr.ts \
            translations/yourappname_it.ts \
            translations/yourappname_ja.ts \
            translations/yourappname_kk.ts \
            translations/yourappname_lt.ts \
            translations/yourappname_nl.ts \
            translations/yourappname_pl.ts \
            translations/yourappname_pt.ts \
            translations/yourappname_ro.ts \
            translations/yourappname_ru.ts \
            translations/yourappname_sk.ts \
            translations/yourappname_sv.ts \
            translations/yourappname_tr.ts

The .ts files are created using the lupdate command, and they are compacted (once filled in by the translators) into .qm files using the lrelease command. This is described in more detail in the coordinator’s section.

More information about internationalizing QT apps can be found on the QT documentation site: http://doc.qt.io/qt-5/qtlinguist-index.html

Internationalizing shell scripts

First, include in your shell script:

 TEXTDOMAINDIR=/usr/share/locale
 TEXTDOMAIN=yourappname

Replace “yourappname” with the name of your shell script.

This instructs the shell interpreter to look for your translations in files such as /usr/share/locale/xx/LC_MESSAGES/yourappname.mo, where “xx” is the locale’s language.

Note that this scheme is different from QT. With QT, each app has its own locale sub-directory containing all the language files for it. With scripts, there is one directory for each language, and the apps scatter their translation files among the language directories. This is also the reason why the language name is not part of the file name for shell script translation files. Compare the location of the French translation file for a QT app and a shell script –

 QT:             /usr/share/yourappname/locale/yourappname_fr.qm
 Script:        /usr/share/locale/fr/LC_MESSAGES/yourappname.mo

Second, in your shell script surround each text string to be translated with $”…”. For example:

 LIST=$"List Devices"

Using $”…” is deprecated in favour of the gettext shell command, but is what we currently use.

Creation of the .po translation files is done through xgettext. The completed files are compacted into .mo files using msgfmt. This is described in detail in the coordinator’s section.

Internationalizing Python Scripts

It’s very much the same idea as with shell scripts.

First, include in your Python script:

 import gettext
 gettext.bindtextdomain('yourappname', '/usr/share/locale')
 gettext.textdomain('yourappname')
 _ = gettext.gettext
 gettext.install('yourappname')

This tells the Python interpreter:

  • import the gettext module
  • where the .mo files will be
  • what the filename part of the .mo file will be
  • sets up a convenient alias for gettext.text calls
  • and makes it available anywhere in the app

So, as for shell scripts, the full path for the object translation files is like:

 /usr/share/locale/fr/LC_MESSAGES/yourappname.mo

Second, surround each text to be translated with _(…). For example:

 xmsg = _('Could not find file')

This is using the shorthand alias established above. Otherwise, one would have to use the more unwieldy:

 xmsg = gettext.gettext('Could not find file')

Creation of the .po files and compacting into .mo files is described in the coordinator’s section.

Leave a Comment

Do NOT follow this link or you will be banned from the site!