JSONC

使用 Lingo.dev CLI 对 JSONC 文件进行 AI 翻译

什么是 JSONC?

JSONC(带注释的 JSON)是对 JSON 的扩展,允许使用单行和多行注释。它通常用于需要便于阅读的人类注释的配置文件中。

例如:

{
  "key1": "Hello, world!", // This is a comment for key1
  "key2": "A simple demo app with JSONC features" /* This is a comment for key2 */,
  // This is a comment for key3
  "key3": "1.0.0",
  // This is a comment for nested values
  "key6": {
    "key7": "Nested value",
  },
  // This key is locked and should not be changed
  "locked_key_1": "This value is locked and should not be changed",
}

什么是 Lingo.dev CLI?

Lingo.dev CLI 是一个免费的开源命令行工具,可通过 AI 翻译应用和内容。它旨在替代传统的翻译管理软件,并可集成到现有的开发流水线中。

如需了解更多信息,请参见 概述

关于本指南

本指南介绍如何使用 Lingo.dev CLI 翻译 JSONC 文件。

你将学习如何:

  • 从零创建项目
  • 配置翻译流水线
  • 使用 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. 创建源内容

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

步骤 5. 创建 bucket

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

    {
      "$schema": "https://lingo.dev/schema/i18n.json",
      "version": "1.10",
      "locale": {
        "source": "en",
        "targets": ["es"]
      },
      "buckets": {
        "jsonc": {}
      }
    }
    
  2. "jsonc" 对象中,定义一个或多个 include 模式的数组:

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

    这些模式定义了需要翻译的文件。

    模式本身:

    • 必须包含 [locale] 作为已配置语言环境的占位符
    • 可以指向文件路径(如 "[locale]/config.jsonc"
    • 可以使用星号作为通配符(如 "[locale]/*.jsonc"

    不支持递归 glob 模式(如 **/*.jsonc)。

步骤 6. 配置 LLM

Lingo.dev CLI 使用大型语言模型(LLM)通过 AI 翻译内容。要使用这些模型,您需要从支持的服务商获取 API 密钥。

为了尽快开始使用,我们推荐选择 Lingo.dev Engine —— 我们自有的托管平台,每月可免费使用 10,000 个 token:

  1. 注册 Lingo.dev 账号

  2. 运行以下命令:

    npx lingo.dev@latest login
    

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

  1. 按照提示操作。

步骤 7. 生成翻译内容

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

npx lingo.dev@latest run

该命令将:

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

首次生成翻译时,会创建一个 i18n.lock 文件。该文件用于记录已翻译的内容,防止后续运行时重复翻译。

示例

en/example.jsonc

{
  "key1": "Hello, world!", // This is a comment for key1
  "key2": "A simple demo app with JSONC features" /* This is a comment for key2 */,
  // This is a comment for key3
  "key3": "1.0.0",
  /* This is a block comment for key4 */
  "key4": "[email protected]",
  /*
   This is a comment for key5
  */
  "key5": "🚀",
  // This is a comment for key6
  "key6": {
    // This is a comment for key7
    "key7": "Nested value",
  },
  // This key is locked and should not be changed
  "locked_key_1": "This value is locked and should not be changed",
  // This key is ignored and should be removed from target locales
  "ignored_key_1": "This value is ignored and should not appear in target locales",
}

es/example.jsonc

{
  "key1": "¡Hola, mundo!", // This is a comment for key1
  "key2": "Una aplicación de demostración simple con características JSONC" /* This is a comment for key2 */,
  // This is a comment for key3
  "key3": "1.0.0",
  /* This is a block comment for key4 */
  "key4": "[email protected]",
  /*
   This is a comment for key5
  */
  "key5": "🚀",
  // This is a comment for key6
  "key6": {
    // This is a comment for key7
    "key7": "Valor anidado",
  },
  // This key is locked and should not be changed
  "locked_key_1": "This value is locked and should not be changed",
}

i18n.json

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

i18n.lock

version: 1
checksums:
  455da9346f4e772000927cd2ff5bb898:
    title: 0468579ef2fbc83c9d520c2f2f1c5059
    description: 6f4922f45568161a8cdf4ad2299f6d23
    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