CMake +Qt6 +lupdate
-
Good afternoon,
I started a new project (didn't choose any language in the Creator), made it all, started to work on the user interface bits and pieces. And decided that I'd need translations. Seems these days one is supposed to useqt_add_translations
so I added the line but cmake complains:
Failed to resolve language code
for every .ts file I specified.Please update CFBundleLocalizations in your Info.plist manually.
Resulting .ts files are just a skeleton:<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> <TS version="2.1"> </TS>
File name pattern is
app_name_xx.ts
where xx = language code (en, de, etc.).How do I go about resolving that?
Many thanks in advance, as always.
Artur
EDIT: Qt 6.6.1, macOS/Windows
-
Try to do a
cmake --build [path-to-folder]build-CamoGenerator-Qt_6_6_1_for_macOS-Debug --target update_translations
first. Maybe it works also in one go, though I'm not sure:
cmake --build [path-to-folder]build-CamoGenerator-Qt_6_6_1_for_macOS-Debug --target all update_translations
The idea is that you don't want to run lupdate on every call, only at certain times, when you're actually ready to do the translations. So that why the target is not a default one.
-
Hi,
Can you show how your
CMakeLists.txt
file looks like ? -
@SGaist Hi, long time no see!
cmake_minimum_required(VERSION 3.5) project(CamoGenerator VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Svg LinguistTools) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Svg LinguistTools) set(DIST_FILES COPYING ) set(PROJECT_SOURCES main.cpp res.qrc MainWindow.h MainWindow.cpp MainWindow.ui PrefsDialog.h PrefsDialog.cpp PrefsDialog.ui AboutDialog.h AboutDialog.cpp AboutDialog.ui ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(CamoGenerator MANUAL_FINALIZATION ${PROJECT_SOURCES} ${DIST_FILES} ) else() add_executable(CamoGenerator ${PROJECT_SOURCES} ) endif() qt_add_translations(CamoGenerator TS_FILES CamoGenerator_en.ts CamoGenerator_pl.ts RESOURCE_PREFIX /i18n SOURCES ${PROJECT_SOURCES} LUPDATE_OPTIONS -source-language en -target-language pl ) target_link_libraries(CamoGenerator PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) target_link_libraries(CamoGenerator PRIVATE Qt${QT_VERSION_MAJOR}::Svg) if(${QT_VERSION} VERSION_LESS 6.1.0) set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER pl.com.trollnet.CamoGenerator) endif() set_target_properties(CamoGenerator PROPERTIES ${BUNDLE_ID_OPTION} MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) include(GNUInstallDirs) install(TARGETS CamoGenerator BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(CamoGenerator) endif()
I also verified that if run manually, with source files specified, lupdate works.
-
qt_add_translations(CamoGenerator TS_FILES CamoGenerator_en.ts CamoGenerator_pl.ts RESOURCE_PREFIX /i18n SOURCES ${PROJECT_SOURCES} LUPDATE_OPTIONS -source-language en -target-language pl )
The
-target-language
option looks wrong, as it makes no sense for CamoGenerator_en.ts. You should be able to just leave it out, actually, as lupdate should be clever enough to figure it out from the file name.For CamoGenerator_en.ts, consider passing it by NATIVE_QS_FILE (at least with Qt 6.6):
qt_add_translations(CamoGenerator TS_FILES CamoGenerator_pl.ts NATIVE_TS_FILE CamoGenerator_en.ts RESOURCE_PREFIX /i18n SOURCES ${PROJECT_SOURCES} LUPDATE_OPTIONS -source-language en )
-
@kkoehne Hi and thank you for the suggestion.
The -target-language and other options are the artefacts of me trying to make it work by specifying extra options I never had to use.
Now with regards to building additional target - build step is:
cmake --build [path-to-folder]build-CamoGenerator-Qt_6_6_1_for_macOS-Debug --target all
Do I need to specify an additional build step now? This used to be more straightforward (and better documented) in the past.
-
Try to do a
cmake --build [path-to-folder]build-CamoGenerator-Qt_6_6_1_for_macOS-Debug --target update_translations
first. Maybe it works also in one go, though I'm not sure:
cmake --build [path-to-folder]build-CamoGenerator-Qt_6_6_1_for_macOS-Debug --target all update_translations
The idea is that you don't want to run lupdate on every call, only at certain times, when you're actually ready to do the translations. So that why the target is not a default one.
-
@kkoehne That did a trick, thank you. I am not sure I appreciate the extra hoops one needs to go through to have the result but I got it working.
NATIVE_TS_FILE
is something I'd need to read on, is there any documentation? I could not find it in the help system.At any rate, marking this as solved, thank you a thousand times!
-
A artwaw has marked this topic as solved on
-
@artwaw said in CMake +Qt6 +lupdate:
I am not sure I appreciate the extra hoops one needs to go through to have the result but I got it working.
Yeah, it's a tad ugly. You should have gotten a warning like
Translation file '`CamoGenerator_en.ts' does not exist. Consider building the target 'update_translations' to create an initial version of that file.
on the first run though?
-
@artwaw said in CMake +Qt6 +lupdate:
Not with my initial setup. After I altered the add_translations line to match your suggestion it indeed popped up but then I was already making changes to the build command.
Weird. I'll try to replicate this...
Btw, seeing your original CMakeLists.txt file: It seems you want to keep compatibility with older Qt 6 versions, and even Qt 5. Please note that qt_add_translations() was only added to Qt 6.2, so you might need to guard it by an Qt version check. And the NATIVE_TS_FILE option only will actually appear in Qt 6.7: https://doc-snapshots.qt.io/qt6-dev/qtlinguist-cmake-qt-add-translations.html
-
@kkoehne Thank you for the docs link - that all makes much more sense now!
As for backward compatibility - no, I don't intent on keeping that, simply didn't clean the autogenerated template yet. I'll bear in mind narrowing the checks to ver=>6.2 though.
Again, many thanks for sharing the knowledge!
-
@kkoehne said in CMake +Qt6 +lupdate:
cool - sounds to become easier and more flexible in 6.7. will test with beta2And the NATIVE_TS_FILE option only will actually appear in Qt 6.7: https://doc-snapshots.qt.io/qt6-dev/qtlinguist-cmake-qt-add-translations.html
I'm developing always in english, where in most cases german is used by customers. Because I'm writing code in 'developer-english' it may happen, that customer requests to change a translation. for these cases and to handle plural forms I always also add an en ts file, so I'm using
.ts / _de.ts / _en.ts / _fr.ts ...
this is working well because translations not found in _en.ts will come from .ts
seems that in this case I don't have to set the NATIVE_TS_FILE, because _en.ts not only contains plural forms ?if setting QT_I18N_LANGUAGES will this work if my (from Qt 5.15 ported) .ts files are in project_dir/translations ?
BTW: the lrelease command is executed automatically if building in QtCreator ?
In 5.15 my workflow was: lupdate, Linguist, lrelease
Now in 6.7 I can do lupdate automatically with add_dependencies(...lupdate) or execute manually with CMD-K cm ...lupdate and lrelease is always done automagically ?