c++qtcmake

Qt with CMake: Translation .ts files are not updated


I want to provide translations to my Qt (version 6.5.2) application, built with CMake. For this, I am using qt_add_translations.

In my source code, I use strings encapsulated with qr(). Now I would expect a .ts file to be generated with entries for all strings to be translated. Similarly, every build process should update this file with new entries.

However, when I first build the application, a .ts file is generated but does not contain any entries. Subsequent builds also do not update the entries.

So, what am I doing wrong and how can I get updates to my .ts file? I boiled it down to this minimal non-working example:

// main.cpp

#include "mainwindow.hpp"
#include <QApplication>
#include <QTranslator>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    QTranslator translator;
    if (translator.load(":/i18n/lang_de")) {
        a.installTranslator(&translator);
    }

    MainWindow w;
    w.show();
    return a.exec();
}
// mainwindow.hpp

#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QMainWindow>

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
};

#endif // MAINWINDOW_HPP
// mainwindow.cpp

#include "mainwindow.hpp"
#include <QLabel>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    QLabel *label = new QLabel(tr("Translate this")); // this string should show up in lang_de.ts
    setCentralWidget(label);
}

MainWindow::~MainWindow() {
}
# CMakeLists.txt

cmake_minimum_required(VERSION 3.25)

project(mnwe VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt6 REQUIRED COMPONENTS Widgets LinguistTools)

add_executable(${CMAKE_PROJECT_NAME} main.cpp mainwindow.hpp mainwindow.cpp)

qt_add_translations(${CMAKE_PROJECT_NAME} TS_FILES lang_de.ts)

target_link_libraries(${CMAKE_PROJECT_NAME} Qt::Widgets)

set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
    WIN32_EXECUTABLE ON
    MACOSX_BUNDLE ON
)
<!-- lang_de.ts -->
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="de_DE">
</TS>

Things I also tried, without success: qt_create_translation, qt_add_lupdate, as well as a bunch of optional arguments to these. Also, this question in the Qt forum seems similar/identical but has no answers.


Solution

  • qt_add_translations(${CMAKE_PROJECT_NAME} TS_FILES lang_de.ts)

    creates the "rule" and a target mnwe_lupdate.

    You have to add that target as dependencies of appropriate target.

    So you might add either:

    add_dependencies(mnwe mnwe_lupdate)
    add_dependencies(mnwe update_translations) # equivalent of all {target}_lupdate
    update_translations
    

    As lupdate parses all files (might be "long"), it is not necessary a good idea to run it for each build. that's why that dependency is not automatically add there.