ID based translation with CMAKE creates superfluous entries in TS file
-
Follow these steps to see the issue. Use Windows OS. Create new "Qt Widgets Application" project. Name it "Testing". Use CMAKE build system. Select "English (United States)" as language (I assume you have this language installed in the system). Use "Desktop Qt 6.6.1 MSVC2019 64bit" kit.
In CMakeLists.txt file replace
qt_create-translation
withqt_add_translation
. The code should look like this (if the project is named "Testing":#qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES}) qt_add_translations(Testing TS_FILES ${TS_FILES} RESOURCE_PREFIX "i18n" LUPDATE_OPTIONS -locations none -no-ui-lines -no-obsolete LRELEASE_OPTIONS -idbased) add_dependencies(Testing Testing_lupdate) add_dependencies(Testing update_translations)
Open
MainWindow.ui
. Go to menu Tools > Form Editor > Form Settings. In new dialog select check boxID-based
. Close the .ui, and build the project. The first time you build the project the fileTesting_en_US.ts
should look like this:<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> <TS version="2.1" language="en_US"> <context> <name>MainWindow</name> <message> <source>MainWindow</source> <translation type="unfinished"></translation> </message> </context> </TS>
Open the .ui file again.
windowTitle
property now hasid
sub-property. Setid
value to "id-title", and build the project again. The .ts file now looks like this:<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> <TS version="2.1" language="en_US"> <context> <name>MainWindow</name> <message id="id-title"> <source>MainWindow</source> <translation type="unfinished"></translation> </message> </context> </TS>
A little bit confusing that there is a context, because ID based translation model should be contextless, but fine, whatever. BUT, now rebuild the project. The .ts file NOW looks like this:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> <TS version="2.1" language="en_US"> <context> <name></name> <message> <source>id-title</source> <translation type="unfinished"></translation> </message> </context> <context> <name>MainWindow</name> <message id="id-title"> <source>MainWindow</source> <translation type="unfinished"></translation> </message> </context> </TS>
Is this a bug, or am I doing something wrong? I don't expect two entries for the same translation term.
This new entry is actually ignored by the code. Add translation to the
<message id="id-title">
entry and it will appear in window title. Entry<source>id-title</source>
is not used. -
Due to your cmake commands, the lupdate is run before the ui file is converted to a ui_.h so the old source is used.
-
@Christian-Ehrlicher I'm not sure that's it. I commented out both
add_dependencies
lines, cleaned the project, deleted the Testing_en_US.ts file, executedcm update_translations
, and then I get proper .ts file. I can execute thiscm update_translations
many times, nothing unusual happens. BUT once I build the project and run thecm
command again I get the faulty .ts file again. I'm not exactly an expert on both CMAKE and Qt, so if there is a proper way to make it work I'd like to see it.I've found this bug report QTBUG-118808. It appears to be fixed in Qt 6.6.3. I'm using Qt Creator 12.0.2 (based on Qt 6.6.0). I guess I'll just have to wait for the next version of Qt Creator.
-
@Dialecticus said in ID based translation with CMAKE creates superfluous entries in TS file:
It appears to be fixed in Qt 6.6.3. I'm using Qt Creator 12.0.2 (based on Qt 6.6.0). I guess I'll just have to wait for the next version of Qt Creator.
I don't see what QtCreator has to do with the Qt version you're using for building your apps though.
-
I used Qt Maintenance Tool, and after 2 x 6.2 GB downloads (the first one ended abruptly while I wasn't watching), and some non-obvious-to-a-beginner hurdles to switch to new toolset, I fixed my problem. Qt 6.6.3 doesn't have the bug.
-
D Dialecticus has marked this topic as solved on