I am trying to write a simple qml application with language translations using CMake and although the application runs, it never shows translations. I'm on a Linux platform with CMake version 3.22.2 and Qt version 3.15.2 Here is the directory structure:
├── CMakeLists.txt
└── src
├── CMakeLists.txt
├── main.cpp
├── silly.qrc
└── qml
├── silly.qml
└── translations
├── qml_de.ts
├── qml_en.ts
└── qml_fr.ts
CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(silly VERSION 1.0.0)
add_subdirectory(src)
src/CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
find_package(Qt5 COMPONENTS Qml Quick LinguistTools REQUIRED)
set(TRANSLATIONS
qml/translations/qml_en.ts
qml/translations/qml_fr.ts
qml/translations/qml_de.ts
)
qt5_create_translation(QM_FILES ${CMAKE_CURRENT_SOURCE_DIR} ${TRANSLATIONS})
qt5_add_resources(QRC_RESOURCES "silly.qrc")
add_executable(silly main.cpp
"${QRC_RESOURCES}"
"${QM_FILES}"
)
target_compile_features(silly PUBLIC cxx_std_17)
set_target_properties(silly PROPERTIES AUTOMOC ON AUTORCC ON)
target_link_libraries(silly PRIVATE Qt5::Quick Qt5::Qml)
target_include_directories(silly
PUBLIC
$<INSTALL_INTERFACE:.>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
)
install(TARGETS silly)
src/main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.addImportPath("qrc:/");
engine.load(QUrl("qrc:/qml/silly.qml"));
return app.exec();
}
src/silly.qrc
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/qml">
<file alias="silly.qml">qml/silly.qml</file>
</qresource>
</RCC>
src/qml/silly.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
visible: true
Button {
anchors.fill: parent
spacing: 20
text: qsTr("Hello")
}
}
src/qml/translations/qml_de.ts
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="de_DE">
<context>
<name>silly</name>
<message>
<location filename="../silly.qml" line="11"/>
<source>Hello</source>
<translation>Hallo</translation>
</message>
</context>
</TS>
src/qml/translations/qml_en.ts
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
<name>silly</name>
<message>
<location filename="../silly.qml" line="11"/>
<source>Hello</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
src/qml/translations/qml_fr.ts
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="fr_FR">
<context>
<name>silly</name>
<message>
<location filename="../silly.qml" line="11"/>
<source>Hello</source>
<translation>Bonjour</translation>
</message>
</context>
</TS>
It builds and runs with no errors, but when I attempt to test with this:
LANGUAGE=fr src/silly
I get a button that says "Hello" instead of a button that says "Bonjour". I've been trying to figure this out for hours which also leads me to the secondary question: how does one generally troubleshoot Qt language translations? This is my first time and I could find no documentation on that.
CodePudding user response:
That's pretty badly documented stuff which works magically with ready-made example using dedicated qmake configuration parameter embed_translations
. I advice you to take a look into the original example's build dir where .qm files and a special qrc file qmake_qmake_qm_files.qrc
get generated.
You don't need to use QTranslator
unless you want to support dynamic language switch. At startup, QML runtime automatically loads a translation file qml_<language_COUNTRY>.qm (qml_xx_XX.qm where xx is ISO639 and XX is optional ISO 3166 code) from the i18n
subdirectory of the root QML file, based on the system language, if it finds one.
You need to get your .qm files to qrc:/qml/i18n/
folder because your main qml file is in qrc:/qml/
.
With CMake
you can do it as follows:
Add a new qrc file, e.g. cmake_qm_files.qrc to your project
<RCC>
<qresource prefix="/qml/i18n">
<file>qml_de.qm</file>
<file>qml_en.qm</file>
<file>qml_fr.qm</file>
</qresource>
</RCC>
Get CMake to copy the qrc file to binary dir where .qm files get created
configure_file(cmake_qm_files.qrc ${CMAKE_BINARY_DIR} COPYONLY)
Get qrc file resource compiled and embedded to your executable
add_executable(silly main.cpp
${QRC_RESOURCES}
${CMAKE_BINARY_DIR}/cmake_qm_files.qrc
)
I typically use QLocale for translation testing as follows:
QGuiApplication app(argc, argv);
QLocale systemLocale = QLocale::system();
QLocale::Language language = systemLocale.language();
QLocale::Country country = systemLocale.country();
qDebug() << "System locale language:" << language << ", country:" << country;
// TEST: change default locale by removing comments below
// language = QLocale::French;
// country = QLocale::France;
language = QLocale::English;
country = QLocale::Australia;
QLocale locale(language, country);
qDebug() << "Changing default locale to language:" << locale.language() << ", country:" << locale.country();
QLocale::setDefault(locale); // TEST: set default locale to something else than system locale
QQmlApplicationEngine engine;