XML
使用 Lingo.dev CLI 对 XML 文件进行 AI 翻译
什么是 XML?
XML(可扩展标记语言,Extensible Markup Language)是一种标记语言,用于定义编码文档的规则,使其既可供人类阅读,也可供机器读取。它常用于配置文件、数据交换和结构化内容。
例如:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<title>Hello, world!</title>
<description>
<summary>Simple demo app</summary>
<details>Basic example content</details>
</description>
<image src="photo.jpg" alt="Example photo" title="Click to view"/>
<link href="example.com" label="Click here">Learn more</link>
<message>
<greeting>Hello there!</greeting>
<body>Welcome to the app</body>
</message>
</root>
什么是 Lingo.dev CLI?
Lingo.dev CLI 是一款免费开源的命令行工具(CLI),可通过 AI 翻译应用和内容。它旨在替代传统的翻译管理软件,并可集成到现有的开发流水线中。
了解更多信息,请参见 概述。
关于本指南
本指南介绍如何使用 Lingo.dev CLI 翻译 XML 文件。
你将学到如何:
- 从零创建项目
- 配置翻译流水线
- 使用 AI 生成翻译
前置条件
要使用 Lingo.dev CLI,请确保已安装 Node.js v18 及以上版本:
❯ node -v
v22.17.0
第 1 步:设置项目
在你的项目目录下,创建一个 i18n.json 文件:
{
"$schema": "https://lingo.dev/schema/i18n.json",
"version": "1.10",
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {}
}
该文件定义了翻译流水线的行为,包括需要翻译的语言以及本地化内容在文件系统中的位置。
如需了解可用属性,请参见 i18n.json。
第 2 步:配置源语言环境
源语言环境 是指你的内容最初编写时所用的语言和地区。要配置源语言环境,请在 i18n.json 文件中设置 locale.source 属性:
{
"$schema": "https://lingo.dev/schema/i18n.json",
"version": "1.10",
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {}
}
源语言环境必须以 BCP 47 语言标签 的形式提供。
有关 Lingo.dev CLI 支持的所有语言环境代码的完整列表,请参见 支持的语言环境代码。
第 3 步:配置目标语言环境
目标语言环境 是指您希望将内容翻译成的语言和地区。要配置目标语言环境,请在 locale.targets 属性中,在 i18n.json 文件中进行设置:
{
"$schema": "https://lingo.dev/schema/i18n.json",
"version": "1.10",
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {}
}
第 4 步:创建源内容
如果尚未创建,请新建一个或多个包含待翻译内容的 XML 文件。这些文件的路径中必须包含源语言环境(例如,作为目录名 en/,或作为文件名的一部分 messages.en.xml)。
第 5 步:创建 bucket
-
在
i18n.json文件中,向buckets对象添加一个"xml"对象:{ "$schema": "https://lingo.dev/schema/i18n.json", "version": "1.10", "locale": { "source": "en", "targets": ["es"] }, "buckets": { "xml": {} } } -
在
"xml"对象中,定义一个或多个include模式的数组:{ "$schema": "https://lingo.dev/schema/i18n.json", "version": "1.10", "locale": { "source": "en", "targets": ["es"] }, "buckets": { "xml": { "include": ["./[locale]/example.xml"] } } }这些模式用于定义需要翻译的文件。
模式本身:
- 必须包含
[locale]作为已配置语言环境的占位符 - 可以指向文件路径(如
"[locale]/config.xml") - 可以使用星号作为通配符(如
"[locale]/*.xml")
不支持递归 glob 模式(如
**/*.xml)。 - 必须包含
第 6 步:配置 LLM
Lingo.dev CLI 使用大型语言模型(LLM)通过 AI 翻译内容。要使用这些模型,您需要从支持的服务商获取 API 密钥。
为了尽快开始使用,我们推荐选择 Lingo.dev Engine —— 我们自有的托管平台,每月可免费使用 10,000 个 token:
-
运行以下命令:
npx lingo.dev@latest login这将打开您的默认浏览器,并要求您进行身份验证。
-
按提示操作。
步骤 7. 生成翻译内容
在包含 i18n.json 文件的目录下,运行以下命令:
npx lingo.dev@latest run
该命令将:
- 读取
i18n.json文件。 - 查找需要翻译的文件。
- 提取文件中的可翻译内容。
- 使用已配置的 LLM 翻译提取的内容。
- 将翻译后的内容写回文件系统。
首次生成翻译时,会创建一个 i18n.lock 文件。该文件用于记录已翻译的内容,防止后续运行时重复翻译。
示例
en/example.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<title>Hello, world!</title>
<description>
<summary>Simple demo app</summary>
<details>Basic example content</details>
</description>
<image src="photo.jpg" alt="Example photo" title="Click to view"/>
<link href="example.com" label="Click here">Learn more</link>
<button type="submit" value="Submit" placeholder="Enter text">Submit</button>
<message>
<greeting>Hello there!</greeting>
<body>Welcome to the app</body>
<signature>Thanks, MyApp Team</signature>
</message>
</root>
es/example.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<title>¡Hola, mundo!</title>
<description>
<summary>Aplicación de demostración simple</summary>
<details>Contenido de ejemplo básico</details>
</description>
<image src="photo.jpg" alt="Foto de ejemplo" title="Haz clic para ver"/>
<link href="example.com" label="Haz clic aquí">Aprender más</link>
<button type="submit" value="Enviar" placeholder="Ingresa texto">Enviar</button>
<message>
<greeting>¡Hola!</greeting>
<body>Bienvenido a la aplicación</body>
<signature>Gracias, Equipo de MyApp</signature>
</message>
</root>
i18n.json
{
"version": "1.10",
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {
"xml": {
"include": ["./[locale]/example.xml"]
}
},
"$schema": "https://lingo.dev/schema/i18n.json"
}
i18n.lock
version: 1
checksums:
ec06a6ebae97ffd5f7afc99d9a8f051b:
root/title: 0468579ef2fbc83c9d520c2f2f1c5059
root/description/summary: f2c85bf6eeebeea33609e04598201bb6
root/description/details: 2ee85b8f2f0f1bc008d9cf1f916cb09c
root/image/%24/src: 3ce26f0a5486adf10e1b7eee1b866a70
root/image/%24/alt: 94058fbed56fffaef2e9bbea59ba4a54
root/image/%24/title: 60487c71b570d9dedca6fddd4a75d16a
root/link/_: e598091d132f890c37a6d4ed94f6d794
root/link/%24/href: 285d79d2783cf0769ab9e767362c1499
root/link/%24/label: 26ce69aad587f70d47e7606436bf1d6d
root/button/_: 7c91ef5f747eea9f77a9c4f23e19fb2e
root/button/%24/type: fa8748b22d5bac98fdcd57e3d6594cf3
root/button/%24/value: 7c91ef5f747eea9f77a9c4f23e19fb2e
root/button/%24/placeholder: 7b5d59cee6952db66043a4b289b51884
root/section/article/paragraph/sentence: 28ca53c71a2e3e3de79c892a9b193b1a
root/meta/%24/name: d097029e873a4b19132e2603bd2c9fe4
root/meta/%24/content: 0811ae3ab84aa87205383c3d8ac42bf3
root/message/greeting: 85559fc839c5181b7958654e62c987d5
root/message/body: ed0d4b1cde20d045f9f8c5007c784b0b
root/message/signature: 181c8c304980949e101865098f548705