0

When building a project in Qt Creator, these lines in the .pro file:

CONFIG += lrelease
CONFIG += embed_translations
QM_FILES_RESOURCE_PREFIX=/lang

will get the .qm translation files embedded in the built executable. The application can then load translations like this:

translator.load(":/lang/file_name.qm")

I am trying to replicate this behaviour when building the project in Visual Studio with the Qt VS Tools extension, to no avail.

I have tried fiddling with the settings available inside the project configuration properties (Qt Project Settings / qmake / Additional project settings) to no avail.

When the project includes any .ts file, Visual Studio adds a "Qt Translation" page to the project properties. Here, the "Run translation tool during build" entry gets the .qm files generated at build time, which takes care of

CONFIG += lrelease

It is also possible to specify the output directory for the .qm files. However, I have found no way to replicate these two:

QM_FILES_RESOURCE_PREFIX=/lang
CONFIG += embed_translations

to get the .qm files embedded as executable resources, available under the ":/lang/" prefix

I am using VS2019 with the latest Qt VS Tools extension. I guess it is not any different with VS2022, since they share the same Qt extension code base.

Gustavo Q
  • 61
  • 5
  • I'm not familiar with the Qt VS Tools, but a solution may be to add the qm file manually to a qrc file once. – m7913d Feb 15 '22 at 10:42
  • @m7913d For this to work, the .qm files must be on a subdirectory of the .qrc resource file. Also, not sure about build order. If lupdate runs after the resource compiler, the resulting embedded language files will be outdated. It is still a possible workaround. My current workaround is commenting the code dealing with translation files and developing without them. I keep a separate .pro file to build from Qt Creator when I need the translations for a release. – Gustavo Q Feb 15 '22 at 11:51
  • Concerning the subdirectory, placing the qrc and qm file in the same directory should just work fine – m7913d Feb 15 '22 at 14:45
  • Sure, I meant it is not possible to have them in a separate unrelated directory. – Gustavo Q Feb 15 '22 at 21:17
  • 1
    @m7913d I have now applied your suggestion as a workaround and can confirm it works. When qmake automatically embeds the translation files in the executable, it removes their extension. To mimic this behaviour when adding the language files as resources manually, add the file name as the alias to each file in the resource editor. – Gustavo Q Feb 19 '22 at 23:11

0 Answers0