Xcode .xcstrings 目录

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

什么是 Xcode .xcstrings 目录?

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

例如:

{
  "sourceLanguage" : "en",
  "strings" : {
    "item_count" : {
      "comment" : "列表中显示的项目数量 - 支持复数形式",
      "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" : "应用程序主屏幕上显示的欢迎消息",
      "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 步:配置目标语言环境

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

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

第 4 步:创建源内容

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

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

第 5 步:创建存储桶

  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 个免费使用的令牌:

  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" : "应用程序主屏幕上显示的欢迎消息",
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "Hello, world!"
          }
        }
      }
    },
    "item_count" : {
      "comment" : "项目数量 - 支持复数形式",
      "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" : "应用程序主页上显示的欢迎消息",
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "Hello, world!"
          }
        },
        "es" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "¡Hola, mundo!"
          }
        }
      }
    },
    "item_count" : {
      "comment" : "项目数量 - 支持复数形式",
      "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