The steps to localize applications include creating translatable applications, preparing them for translation, translating the strings, and creating runtime translation files for the released application.
Qt Quick and Qt C++ applications use the same underlying localization system:
lupdate
,
lrelease
, and the translation source (TS) files and QM files that they generate. You use the same tools for both QML and C++ code, as described in the
Qt Linguist 手册
.
You can even have user interface strings in QML and C++ source files in the same application. The system will create a single combined translation file and the strings are accessible from QML and C++ code.
要本地国际化应用程序:
要启用
lupdate
and
lrelease
以生成 TS 和 QM 文件,更新应用程序工程文件以指定包含要翻译文本的源文件。
Use ISO language and country codes in the TS file name to determine the language to load at runtime. For more information, see 启用翻译 .
The
lupdate
tool extracts user interface strings from your application. It expects all source code to be encoded in UTF-8 by default. For more information, see
编码
.
添加
qt_add_translations
命令到
CMakeLists.txt
文件以创建用于更新 TS 文件的目标,并将它们变换成 QM 文件。
When using qmake, set a conditional statement to hide the QML source from the compiler that you list in the
SOURCES
or
HEADERS
entry in the .pro file.
The SOURCES variable is intended for C++ source files. If you list QML or JavaScript source files there, the compiler tries to build them as though they are C++ files. As a workaround, you can use an
lupdate_only{...}
conditional statement so the
lupdate
tool sees the .qml files but the C++ compiler ignores them.
For example, the following .pro file snippet specifies two .qml files in the application.
lupdate_only{ SOURCES = main.qml \ MainPage.qml }
You can also specify the .qml source files with a wildcard match. The search is not recursive, so you need to specify each directory that contains source files that contain UI strings:
lupdate_only{ SOURCES = *.qml \ *.js \ content/*.qml \ content/*.js }
Place the
.qm
files required for the application in a location where the loader code using
QTranslator
can find them. Typically, you specify a path relative to
QCoreApplication::applicationDirPath
().
In addition to the application's QM files, you need to deploy the QM files for the Qt modules that you use in the application, unless they are installed on the system.
The QM files are split up by module and there is a so-called meta catalog file which includes the QM files of all modules. However, you only need to deploy the QM files for the modules that you use in the application.
可以使用
lconvert
tool in the deploy step to concatenate the required QM files into one file that matches the meta catalog file. For example, to create a German translation file for an application that uses the
Qt Core
,
Qt GUI
,和
Qt Quick
模块,运行:
lconvert -o installation_folder/qt_de.qm qtbase_de.qm qtdeclarative_de.qm
Qt modules contain several thousands of strings that also need to be translated into the languages that you are targeting. You can find a number of TS files in the qttranslations repository. Before you start translating Qt, read the wiki page 将 Qt 翻译成其它语言 .
可以使用 QLibraryInfo::path () to locate the translations for the Qt modules that your application uses. You can request the path to the translations at run-time by passing QLibraryInfo::TranslationsPath to this function.
Qt 翻译分类位于
qttranslations
存储库。
警告: Qt translations are contributed by the Qt community, and provided without any guarantees. Translations might be missing, outdated, or entirely incorrect, up to the point of being malicious. It is recommended that you audit any translations you ship.
In Qt 4, there is one big, monolithic
.qm
file per locale. For example, the file
qt_de.qm
contains the German translation of all libraries.
The
qt_
meta catalog contains the still-existing Qt translations that were included in the
qt_
catalog in Qt 4. It was created to make porting applications from Qt 4 to Qt 5 easier. The meta catalog depends on translations that might be absent, because they belong to unnecessary or deprecated modules, which might cause the loading of the translations to fail. If you use modules that are new in Qt 5 or later in your application, you must specify the names of the catalogs for those modules even if you use the meta catalog.
The following table lists the translation catalogs available for the Qt modules and tools in Qt.
Qt 模块或工具 | 分类 |
---|---|
Qt Bluetooth |
qtconnectivity
|
Qt Concurrent |
qtbase
|
Qt Core |
qtbase
|
Qt D-Bus |
qtbase
|
Qt Designer |
designer
|
Qt GUI |
qtbase
|
Qt Help |
qt_help
|
Qt Linguist |
linguist
|
Qt Location |
qtlocation
|
Qt Multimedia |
qtmultimedia
|
Qt Network |
qtbase
|
Qt NFC |
qtconnectivity
|
Qt Print Support |
qtbase
|
Qt Qml |
qtdeclarative
|
Qt Quick |
qtdeclarative
|
Qt Quick Controls |
qtdeclarative
|
Qt Quick Widgets |
qtdeclarative
|
Qt Serial Port |
qtserialport
|
Qt SQL |
qtbase
|
Qt Widgets |
qtbase
|
Qt WebSockets |
qtsockets
|
Qt WebEngine |
qtwebengine
|
For example, to locate translations for essential Qt modules, such as Qt Core, Qt GUI, Qt Network, and Qt Widgets, add the following code to the
main()
函数:
QTranslator qtTranslator; if (qtTranslator.load(QLocale::system(), u"qtbase"_s, u"_"_s, QLibraryInfo::path(QLibraryInfo::TranslationsPath))) { app.installTranslator(&qtTranslator); }编写翻译源代码 复数形式翻译规则