英文:
Qt with CMake: Translation .ts files are not updated
问题
我想为我的Qt应用程序(版本6.5.2)提供翻译,该应用程序是使用CMake构建的。
为此,我使用了qt_add_translations
。
在我的源代码中,我使用了用qr()
封装的字符串。现在,我希望生成一个.ts
文件,其中包含所有需要翻译的字符串的条目。类似地,每次构建过程都应该更新此文件中的条目。
然而,当我首次构建应用程序时,会生成一个.ts
文件,但不包含任何条目。随后的构建也不会更新这些条目。
那么,我做错了什么,如何才能更新我的.ts
文件呢?
我将其简化为以下这个最小的不起作用的示例:
// 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")); // 这个字符串应该出现在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>
我还尝试了以下方法,但没有成功:qt_create_translation,qt_add_lupdate,以及这些方法的一些可选参数。
此外,Qt论坛中的这个问题看起来很相似/相同,但没有答案。
英文:
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.
答案1
得分: 1
qt_add_translations(${CMAKE_PROJECT_NAME} TS_FILES lang_de.ts)
创建了一个"rule"和一个目标mnwe_lupdate
。
您需要将该目标添加为适当目标的依赖项。
因此,您可以添加以下内容之一:
add_dependencies(mnwe mnwe_lupdate)
add_dependencies(mnwe update_translations) // 等同于所有{target}_lupdate
update_translations
由于lupdate
解析所有文件(可能会很"长"),所以每次构建都运行它并不是一个好主意。这就是为什么该依赖项不会自动添加的原因。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论