创建翻译文件

应用程序中需要翻译的大部分文本由单个词或短语组成。这些通常出现在窗口标题、菜单项、工具提示以及按钮、复选框和单选按钮的标签中。

开发人员会在 Qml 和 C++ 源代码中将这些短语标记为可翻译。Qt 工具会为每个短语提供上下文信息,以帮助翻译人员理解其含义。开发人员还可以在短语中添加注释。

翻译文件包含应用程序中所有用户可见的文本和Ctrl键快捷键,以及这些文本的翻译内容。

要创建翻译文件:

  1. 运行lupdate 生成第一组翻译源(TS)文件,其中包含所有用户可见的文本,但不包含翻译内容。
  2. 将 TS 文件交给翻译人员,由他们使用Qt Linguist 添加翻译。Qt Linguist 会标示已更改和已删除的源文本。
  3. 运行lupdate 以整合添加到应用程序中的任何新文本。lupdate 会将应用程序中的用户可见文本与翻译内容进行同步。TS 文件中的现有翻译将被保留。
  4. 运行lrelease 以读取 TS 文件,并生成应用程序在运行时使用的 QM 文件。

为了使lupdate 成功运行,它必须知道需要生成哪些翻译文件。请在应用程序的 Qt 项目文件中指定这些文件。

使用 CMake 构建时,您需要通过CMake 命令添加目标,这些目标负责创建或更新 TS 文件,并将其转换为 QM 文件。在构建这些目标时,系统会自动生成翻译文件。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.