Share your knowledge at the LQ Wiki.
Go Back > Blogs > Michael Uplawski
User Name


Rate this Entry

Qt: Embedding Translations in the program-file

Posted 03-29-2023 at 02:41 PM by Michael Uplawski
Updated 03-30-2024 at 04:49 AM by Michael Uplawski (German version, French version)

Embedding translations in Qt-programs


Several techniques allow the internationalization of Qt -program.
I have worked out the procedure explained on this page, to embed translations in an executable file, so that they will be immediately available without having to keep track of the locations of *.qm files on an end-users system.
I cannot say that my procedure were special in any way, because I am only using what the Qt development environment proposes anyway. I am – however – not following the recommendations that you find on the Web and in the help-files that accompany the translation-tools, mainly because I do not understand them!
You should probably compare my explanations here to those that you find elsewhere, then do as you please...
My Qt-version is at the time of this writing: 6.4

Preliminary work

These are steps which I will not explain further. For more details see the QtLinguist manual .
  1. The C++ developer who uses the QTranslator object must hand strings to the QObject::tr() method (or QCoreApplication::translate()), so that they can be translated.
  2. The same developer must ensure that the second parameter to the load() method of the QTranslator-object is the precise string ":i18n", the default resource-prefix for translation-files. For example:
     [[maybe_unused]] bool isso = translator.load(QString("project_") + locale, ":i18n");
  3. A call to the lupdate utility will extract all translatable strings and write them to XML-files with the extension *.ts (a newer utility “lupdate-pro” should achieve the same, but I know nothing about that).
  4. The *.ts files can be given to a translator who will enter a translation to each of the strings listed. Or a translator can use the helper application Qt-Linguist to do the same while taking into consideration the context of each text-snippet in the program.
The one step which is missing to make translations available to the application, concerns the compilation of each *.ts file into a binary Qt message (*.qm) file. This will be part of the following procedure.

Preparing the project file (*.pro)

In the project file, you name the original translation-files:
       TRANSLATIONS += project_name_fr_FR.ts \
The compilation of the translations into *.qm files will be done by calling the helper program lrelease . The call of lrelease and the fact that the translations shall be embedded in the executable, necessitate additional values in the variable CONFIG, in the same project-file:
       CONFIG += lrelease embed_translations
       TRANSLATIONS += project_name_fr_FR.ts \

Preparing the build

To ensure that no residue of previous compilations interferes and if a Makefile is present, run
 :~/[project]$ make distclean
Now re-create the Makefile by calling qmake :
 :~/[project]$ qmake
The changes made in the project-file will have as consequence that additional files will be created. The first one is a default resource-file qmake_qmake_qm_files.qrc that you can see after running qmake. It contains the future location of the compiled *.qm files:
      <!DOCTYPE RCC><RCC version="1.0">
       <qresource prefix="i18n">
         <file alias="project_fr_FR.qm">/project/.qm/project_fr_FR.qm</file>
         <file alias="project_de_DE.qm">/project/.qm/project_de_DE.qm</file>
The hidden directory .qm will be created upon running make, as will the *.qm-files which correspond to the translations. The resource-prefix " i18n " is the same that serves as second parameter to the method QTranslator::load() (see above).

Running make

Now that the Makefile and the resource-file are created, you can build the program by calling
:~/[project]$ make .
This will first find the lrelease tool, compile the *.qm-files and store them in the hidden directory .qm, then create one new file *.cpp and one new header *.h in the source-directory of the project. Eventually the translations will thus be compiled and linked in the executable program-file.

If you like it complicated

Much of the above procedure relies on the defaults provided by the Qt framework. If you prefer, you can modify some of the parameters to
  • work with different file-paths and directories
  • use a different resource prefix or real file-path instead of :i18n
  • name and use a different resource-file
  • skip the call to lrelease to use pre-compiled *.qm-files
I will though not help you with that.
Attached Images
File Type: jpg lq_sc_upsslideshow.jpg (199.1 KB, 10 views)
File Type: png lq_th_sc_upsslideshow.png (10.9 KB, 113 views)
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 01:24 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration