JSON
使用 Lingo.dev CLI 进行 JSON 文件的 AI 翻译
什么是 JSON?
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人类阅读和书写,也易于机器解析和生成。它通常用于配置文件、数据存储和 API 通信。
例如:
{
"title": "Hello, world!",
"description": "A simple demo app",
"author": {
"name": "John Doe"
},
"messages": [
"Welcome to MyApp",
"Hello, world!"
],
"config": {
"theme": {
"primary": "Blue theme"
}
}
}
什么是 Lingo.dev CLI?
Lingo.dev CLI 是一个免费的开源命令行工具,用于通过 AI 翻译应用程序和内容。它旨在取代传统的翻译管理软件,同时与现有的流水线集成。
要了解更多,请参阅 概述。
关于本指南
本指南解释了如何使用 Lingo.dev CLI 翻译 JSON 文件。
您将学习如何:
- 从头开始创建项目
- 配置翻译流水线
- 使用 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 步:配置目标语言环境
目标语言环境 是您希望将内容翻译成的语言和地区。要配置目标语言环境,请在 i18n.json 文件中设置 locale.targets 属性:
{
"$schema": "https://lingo.dev/schema/i18n.json",
"version": "1.10",
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {}
}
第 4 步:创建源内容
如果尚未创建,请创建一个或多个包含待翻译内容的 JSON 文件。这些文件的路径中必须包含源语言环境(例如,作为目录名称如 en/ 或作为文件名的一部分如 messages.en.json)。
第 5 步:创建存储桶
-
在
i18n.json文件中,向buckets对象添加一个"json"对象:{ "$schema": "https://lingo.dev/schema/i18n.json", "version": "1.10", "locale": { "source": "en", "targets": ["es"] }, "buckets": { "json": {} } } -
在
"json"对象中,定义一个或多个include模式的数组:{ "$schema": "https://lingo.dev/schema/i18n.json", "version": "1.10", "locale": { "source": "en", "targets": ["es"] }, "buckets": { "json": { "include": ["./[locale]/example.json"] } } }这些模式定义了需要翻译的文件。
模式本身:
- 必须包含
[locale]作为配置语言环境的占位符 - 可以指向文件路径(例如,
"[locale]/config.json") - 可以使用星号作为通配符占位符(例如,
"[locale]/*.json")
不支持递归的 glob 模式(例如,
**/*.json)。 - 必须包含
第 6 步:配置 LLM
Lingo.dev CLI 使用大型语言模型(LLM)通过 AI 翻译内容。要使用这些模型之一,您需要一个支持提供商的 API 密钥。
为了尽快开始,我们推荐使用 Lingo.dev Engine —— 我们自己的托管平台,每月提供 10,000 个免费令牌:
-
运行以下命令:
npx lingo.dev@latest login这将打开您的默认浏览器并要求您进行身份验证。
-
按照提示操作。
第 7 步:生成翻译
在包含 i18n.json 文件的目录中,运行以下命令:
npx lingo.dev@latest run
此命令将执行以下操作:
- 读取
i18n.json文件。 - 查找需要翻译的文件。
- 从文件中提取可翻译的内容。
- 使用配置的 LLM 翻译提取的内容。
- 将翻译后的内容写回文件系统。
首次生成翻译时,会创建一个 i18n.lock 文件。此文件记录已翻译的内容,防止在后续运行中进行不必要的重复翻译。
示例
en/example.json
{
"title": "Hello, world!",
"description": "A simple demo app",
"version": "1.0.0",
"support_email": "[email protected]",
"homepage": "https://lingo.dev",
"deprecated": null,
"empty": "",
"emoji": "🚀",
"author": {
"name": "John Doe"
},
"contributors": [
{ "name": "Alice" },
{ "name": "Bob" }
],
"messages": [
"Welcome to MyApp",
"Hello, world!"
],
"config": {
"theme": {
"primary": "Blue theme"
}
},
"mixed_array": [
"Mixed content here",
42,
true,
{
"nested_message": "Nested text"
}
],
"locked_key_1": "This value is locked and should not be changed",
"ignored_key_1": "This value is ignored and should not appear in target locales"
}
zh/example.json
{
"title": "你好,世界!",
"description": "一个简单的演示应用",
"version": "1.0.0",
"support_email": "[email protected]",
"homepage": "https://lingo.dev",
"deprecated": null,
"empty": "",
"emoji": "🚀",
"author": {
"name": "约翰·多伊"
},
"contributors": [
{ "name": "爱丽丝" },
{ "name": "鲍勃" }
],
"messages": [
"欢迎使用 MyApp",
"你好,世界!"
],
"config": {
"theme": {
"primary": "蓝色主题"
}
},
"mixed_array": [
"这里是混合内容",
42,
true,
{
"nested_message": "嵌套文本"
}
],
"locked_key_1": "This value is locked and should not be changed"
}
i18n.json
{
"version": "1.10",
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {
"json": {
"include": ["./[locale]/example.json"],
"lockedKeys": ["locked_key_1"]
}
},
"$schema": "https://lingo.dev/schema/i18n.json"
}
i18n.lock
version: 1
checksums:
455da9346f4e772000927cd2ff5bb898:
title: 0468579ef2fbc83c9d520c2f2f1c5059
description: 49f8864eb0e53903f04532bf33e1e4fa
version: 54a9e730e88fb16291b852274d433923
support_email: 10627fcc465897af0f5e1bba042685f9
emoji: b328c432cee108a87a92f05258b6a651
author/name: febee8e9ab40b2fe5106d72675228d00
contributors/0/name: e80d4063a32adaad7b0a82b0bcc10551
contributors/1/name: b2bca2fa3c890618e56d07473f26ead3
messages/0: d1c3a9f35e377554a4ccaa467ca26614
messages/1: 0468579ef2fbc83c9d520c2f2f1c5059
config/theme/primary: 7535a3779d6934ea8ecf18f5cb5b93fd
mixed_array/0: 001b5b003d96c133534f5907abffdf77
mixed_array/3/nested_message: 5f0782dfc5993e99890c0475bc295a30