本页内容

使用 lconvert

lconvert 命令行工具用于过滤和转换翻译数据文件。

它支持以下文件格式:

  • pot - GNU Gettext 本地化模板文件
  • qph -Qt Linguist 短语库
  • ts - Qt 翻译源文件
  • po - GNU Gettext 本地化文件
  • qm - 已编译的 Qt 翻译
  • xlf - XLIFF 本地化文件

lconvert 语法

lconvert [options] <infile> [<infile>...]

其中:

  • options 表示一个或多个lconvert 选项。
  • infile 是一个输入文件。您可以指定多个输入文件。

如果指定了多个输入文件,它们将被合并,且后面的文件中的翻译内容优先。

要查看最新的lconvert 帮助,请输入:

lconvert -help

lconvert 选项

选项操作
-h
-help
Display up-to-date help information and exit.
-i <infile>
-input-file <infile>
Specify an input file. Use this option if <infile> starts with a dash. Use this option several times to merge inputs. May be - (standard input) for use in a pipe.
-o <outfile>
-output-file <outfile>
Specify an output file. Default is - (standard output).
-if <informat>
-input-format <format>
Specify input format for subsequent input files. The format is auto-detected from the file name and defaults to ts.
-of <outformat>
-output-format <outformat>
Specify output format. See -if.
-drop-tags <regexp>在编写TS 或XLIFF 文件时,省略命名的额外标签。您可以多次指定此选项。
-drop-translations删除现有翻译并将状态重置为unfinished 。这意味着--no-obsolete 。
-source-language <language>[_<region>]指定或覆盖源字符串的语言。若未指定且文件中尚未命名,则默认为 POSIX。
-target-language <language>[_<region>]指定或覆盖翻译的语言。默认情况下,目标语言从文件内容中读取,或根据文件名推测。
-no-obsolete删除过时的消息。
-no-finished剔除已完成的消息。
-no-untranslated剔除未翻译的消息。
-sort-contexts在生成的TS 文件中按字母顺序对上下文进行排序。
-sort-messages在TS 文件中,按字母顺序对上下文中的消息进行排序。
-locations {absolute|relative|none}覆盖源代码引用在TS 文件中的保存方式。默认值为absolute 。
-no-ui-lines从对UI 文件的引用中省略行号。
-pluralonly省略非复数形式的提示信息。
-verbose解释正在执行的操作。

示例

将 TS 文件转换为 XLIFF

要将单个 TS 文件转换为 XLIFF,请在终端中运行以下命令:

lconvert -o myapp_de.xlf myapp_de.ts

合并多个 QM 文件

以下命令可将多个QM文件合并为full_de.qm :

lconvert -o full_de.qm qtbase_de.qm myapp_de.qm mylib_de.qm

在 CMake 中使用 lconvert

要在配置或构建 CMake 项目时调用 `lconvert `,请加载 `Qt6LinguistTools ` 软件包,并使用 `$<TARGET_FILE_NAME:Qt6::lconvert> ` 来定位 `lconvert ` 可执行文件。

以下示例添加了一个名为xlf_de 的自定义目标,用于将单个 TS 文件转换为 XLIFF。

find_package(Qt6 REQUIRED COMPONENTS LinguistTools)

add_custom_command(
    OUTPUT myapp_de.xlf
    COMMAND $<TARGET_FILE:Qt6::lconvert> -o myapp_de.xlf myapp_de.ts
    DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/myapp_de.ts"
    VERBATIM
)

add_custom_target(xlf_de
    DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/myapp_de.xlf"
)

PO 格式专用选项

在转换为 PO 格式时,您可以通过在po-flags 的额外元数据字段中设置no-wrap 标志来禁用自动换行。默认情况下,超过 79 个字符的行会自动换行。您可以在源代码中使用//~ 元字符串(例如//~ po-flags no-wrap )设置此字段,或者直接在 TS 文件中使用<extra-po-flags> 元素进行设置。

其他 PO 标志(参见GNU gettext 文档)在 TS 与 PO 格式之间转换时会被保留,但不会影响lconvert 的处理。

注意: 只有在从 TS 格式直接转换为 PO 格式时,po-flags 元数据 才会被保留。

© 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.