JSON

使用 Lingo.dev CLI 进行 JSON 文件的 AI 翻译

什么是 JSON?

JSON(JavaScript 对象表示法)是一种轻量级的数据交换格式,便于人类阅读和编写,也易于机器解析和生成。它常用于配置文件、数据存储和 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 步:配置目标语言环境

目标语言环境 指的是您希望将内容翻译成的语言和地区。要配置目标语言环境,请在 locale.targets 属性中设置,并写入 i18n.json 文件:

{
  "$schema": "https://lingo.dev/schema/i18n.json",
  "version": "1.10",
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {}
}

第 4 步:创建源内容

如果尚未创建,请新建一个或多个包含待翻译内容的 JSON 文件。这些文件的路径中必须包含源语言环境(例如,作为目录名 en/,或作为文件名的一部分 messages.en.json)。

第 5 步:创建 bucket

  1. i18n.json 文件中,向 buckets 对象添加一个 "json" 对象:

    {
      "$schema": "https://lingo.dev/schema/i18n.json",
      "version": "1.10",
      "locale": {
        "source": "en",
        "targets": ["es"]
      },
      "buckets": {
        "json": {}
      }
    }
    
  2. "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 tokens:

  1. 注册 Lingo.dev 账号

  2. 运行以下命令:

    npx lingo.dev@latest login
    

    这将打开您的默认浏览器,并要求您进行身份验证。

  3. 按照提示操作。

步骤 7. 生成翻译内容

在包含 i18n.json 文件的目录下,运行以下命令:

npx lingo.dev@latest run

该命令将:

  1. 读取 i18n.json 文件。
  2. 查找需要翻译的文件。
  3. 提取文件中的可翻译内容。
  4. 使用已配置的 LLM 翻译提取的内容。
  5. 将翻译后的内容写回文件系统。

首次生成翻译时,会创建一个 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"
}

es/example.json

{
  "title": "¡Hola, mundo!",
  "description": "Una aplicación de demostración simple",
  "version": "1.0.0",
  "support_email": "[email protected]",
  "homepage": "https://lingo.dev",
  "deprecated": null,
  "empty": "",
  "emoji": "🚀",
  "author": {
    "name": "Juan Pérez"
  },
  "contributors": [
    { "name": "Alicia" },
    { "name": "Roberto" }
  ],
  "messages": [
    "Bienvenido a MyApp",
    "¡Hola, mundo!"
  ],
  "config": {
    "theme": {
      "primary": "Tema azul"
    }
  },
  "mixed_array": [
    "Contenido mixto aquí",
    42,
    true,
    {
      "nested_message": "Texto anidado"
    }
  ],
  "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