Xcode .xcstrings 目录

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

什么是 Xcode .xcstrings 目录?

Xcode .xcstrings 目录是 Apple 在 Xcode 15 中引入的现代本地化格式。它将所有本地化内容存储在一个 JSON 文件中,支持复数形式、设备差异和翻译元数据。

例如:

{
  "sourceLanguage" : "en",
  "strings" : {
    "item_count" : {
      "comment" : "Number of items displayed in the list - supports pluralization",
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "variations" : {
            "plural" : {
              "one" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "1 item"
                }
              },
              "other" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "%d items"
                }
              },
              "zero" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "No items"
                }
              }
            }
          }
        }
      }
    },
    "welcome_message" : {
      "comment" : "Welcome message shown on the app's home screen",
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "Hello, world!"
          }
        }
      }
    }
  }
}

什么是 Lingo.dev CLI?

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

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

关于本指南

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

你将学到如何:

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

如果尚未创建,请新建一个包含待翻译内容的 .xcstrings 文件。

注意: 在翻译过程中,源内容文件将被覆盖,包含翻译后的内容(以及源内容)。

第 5 步:创建 bucket

  1. i18n.json 文件中,在 buckets 对象下添加一个 "xcode-xcstrings" 对象:

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

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

    这些模式用于定义需要翻译的文件,可以:

    • 指定具体的文件路径(如 "some/dir/file.xcstrings"
    • 使用星号作为通配符(如 "some/dir/*.xcstrings"

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

第 6 步:配置 LLM

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

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

  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 文件。该文件用于记录已翻译的内容,防止后续运行时重复翻译。

示例

example.xcstrings(翻译前)

{
  "sourceLanguage" : "en",
  "strings" : {
    "welcome_message" : {
      "comment" : "Welcome message shown on the app's home screen",
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "Hello, world!"
          }
        }
      }
    },
    "item_count" : {
      "comment" : "Number of items - supports pluralization",
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "variations" : {
            "plural" : {
              "zero" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "No items"
                }
              },
              "one" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "1 item"
                }
              },
              "other" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "%d items"
                }
              }
            }
          }
        }
      }
    }
  }
}

example.xcstrings(翻译后)

{
  "sourceLanguage" : "en",
  "strings" : {
    "welcome_message" : {
      "comment" : "Welcome message shown on the app's home screen",
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "Hello, world!"
          }
        },
        "es" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "¡Hola, mundo!"
          }
        }
      }
    },
    "item_count" : {
      "comment" : "Number of items - supports pluralization",
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "variations" : {
            "plural" : {
              "zero" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "No items"
                }
              },
              "one" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "1 item"
                }
              },
              "other" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "%d items"
                }
              }
            }
          }
        },
        "es" : {
          "variations" : {
            "plural" : {
              "zero" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "Sin elementos"
                }
              },
              "one" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "1 elemento"
                }
              },
              "other" : {
                "stringUnit" : {
                  "state" : "translated",
                  "value" : "%d elementos"
                }
              }
            }
          }
        }
      }
    }
  }
}

i18n.json

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

i18n.lock

version: 1
checksums:
  d3330df55a54ce785f378e7388282a96:
    welcome_message: 57da71b2c89eaddcbca81b3c3766deed
    item_count/zero: 955c31a42e9ff663a49e69e944c8c537
    item_count/one: 154c8eed472b867c8d5af66d162abd2c
    item_count/other: ecb3f18dc6a5e7b3e1d9afe7ef62cf07
  614e9d20c639fed18ee30b9f9637c83d:
    item_count/one: d25ee41f72a2ea94ebc6debc85b9e573
    item_count/other: ecb3f18dc6a5e7b3e1d9afe7ef62cf07
    item_count/zero: 955c31a42e9ff663a49e69e944c8c537
    welcome_message: 0468579ef2fbc83c9d520c2f2f1c5059