Ben Pyton
Ben Pyton

Reputation: 348

How to add sources from qml modules to qt_add_translations in Qt6 CMake?

I am having trouble with setting up translation files in my Qt6 application using CMake.

I think my issue is not restricted to qml files, but I will use here only qml files to keep my case simple.

I'm working on a project with submodules to organize my cpp and qml files.
As an example, lets say I have a root project called appTestLocalization containing a Main.qml and a module MyModule in a directory MyModule containing a MyText.qml.
Each of the qml files contains one qsTr() in them.

I want to add localizations to my app, and following the qt doc I add a qt_add_translations() like that in the CMakeLists.txt of the root project:

qt_add_translations(appTestLocalization TS_FILES i18n/lang_fr_FR.ts)

Then in Qt Creator I do Ctrl+K and I run cm update_translations (or cm appTestLocalization_lupdate but the result is the same)

However, the resulting ts files contain only the qsTr from the Main.qml. So I've tried to add the module directory manually like that but the result is the same:

qt_add_translations(appTestLocalization 
INCLUDE_DIRECTORIES
    MyModule
TS_FILES 
    i18n/lang_fr_FR.ts)

I also tried to add a translation in the module's CMakeLists.txt too:

qt_add_translations(mymodule TS_FILES ${CMAKE_SOURCE_DIR}/i18n/lang_fr_FR.ts)

But it overwrites the translations from the the root project...

How could I add module files to the Qt translation process?

EDIT:

I know I can add the files I want using the SOURCE argument like that:

qt_add_translations(appTestLocalization
    #SOURCES
        Main.qml
        MyModule/MyLabel.qml
    TS_FILES
        i18n/lang_fr_FR.ts
)

But it seems a "wrong" way as we could have a lot of files in several modules. This would be a nightmare to maintain and I would like to avoid that.

I have also tried passing -recursive to lupdate despite it should be the default, but it didn't work either:

qt_add_translations(appTestLocalization
    LUPDATE_OPTIONS -recursive
    TS_FILES
        i18n/lang_fr_FR.ts
)

Upvotes: 0

Views: 657

Answers (1)

Jürgen Lutz
Jürgen Lutz

Reputation: 806

As mentioned by @smr you can scan the files to translate recursive. This is how we add translations to our application also build up of a bunch of modules:

file(GLOB_RECURSE QML_SOURCES RELATIVE ${CMAKE_SOURCE_DIR} *.qml)

qt_add_translations(${EXECUTABLE_NAME} TS_FILES
    ${EXECUTABLE_NAME}_de_DE.ts
    ${EXECUTABLE_NAME}_en_EN.ts
    SOURCES ${QML_SOURCES}
    RESOURCE_PREFIX "/translations"
    LUPDATE_OPTIONS -no-obsolete
)

Upvotes: 1

Related Questions