qtqt-linguist

How do I start using QtLinguist to translate my QtCreator project?


I have a simple .pro Qt Creator project. I have most, if not all, of my application text in Qt .ui XML form files. I want to use Qt Linguist to translate text in that project, but I have no idea how to start. There's no useful "Getting started with Qt Linguist" article on the internet. Page proudly calling itself Qt Linguist Manual is also not very helpful as it only contains generic info, not practical hints.

I found these items in Qt Creator:

image description

I am not so sure what to do to make them work and what is their relation to Qt Linguist, which is totally standalone application. I opened Qt Linguist and it doesn't even contain any File -> New... item menu, which is where I would start.


Solution

  • In the beginning it requires quite some work, but then it is all automatic.

    Source code

    In your source file you must put translatable strings inside a tr() function. All QObject derived classes have their own tr() function. For strings inside that class use the tr function of that class: so in stead of

    QString str ="string to be translated";
    

    use

    QString str = MyClass::tr("string to be translated");
    

    If the class has no tr() function use

    QString str = QOject::tr("string to be translated");
    

    Qt Creator

    In Qt Creator you go to Tools/External/Configure... and fill out the location of lupdate and lrelease. You can hard code their location or use macro's. These utilities are located in a bin subdir of the Qt install location.

    ts files

    Suppose you want to translate from English to Dutch. You will need two files. We will call them: myapp_en.ts and myapp_nl.ts . In principle you do not need to include the English ts file but it helps to treat English on the same footing as another language.

    Project file

    add to the pro file

    TRANSLATIONS+=  /path_to_/myapp_en.ts
    
    TRANSLATIONS+=  /path_to_/myapp_nl.ts
    

    First-time ts files

    The first time you run lupdate the ts files will be created.

    Actual translation

    Open the ts file with Qt Linguist. You can set some properties of this file. All the strings that need to be translated should now be visible in the Qt Linguist view on the ts file. You can insert the translations. And save the modified ts file.

    Update ts files

    Any time you have changed your source code you should run lupdate. Add the translations of any new strings with Qt Linguist and run lupdate and then lrelease. The program lrelease converts the text files to compressed binary files (extension "qm").

    Load translation

    The only thing left now is to tell your application to load a specific translation (that is to load a specific qm file). You do this with the Qt class QTranslator with excellent documentation.