translate_strings

一个 Xcode 工具,可以使用 DeepL、Anthropic Claude 或 OpenAI GPT 服务自动将语言翻译添加到您的 Strings Catalog 文件。

入门

要使用默认的 DeepL 翻译将您应用中的所有字符串翻译成日语,请调用

strings_catalog_translate -k [API_KEY] -t ja

确保初始 Strings Catalog 文件已添加到您的 Xcode 项目。 更多详细信息可以在这里找到。

默认情况下,该命令将从运行它的路径读取 Localizable.xcstrings 文件,并使用翻译修改它。

用法

OVERVIEW: A utility for language translation of Xcode Strings Catalogs.

USAGE: strings_catalog_translate <subcommand>

OPTIONS:
  --version               Show the version.
  -h, --help              Show help information.

SUBCOMMANDS:
  deepl                   Translate Xcode Strings Catalog using DeepL service.
  openai                  Translate Xcode Strings Catalog using OpenAI service.
  gemini                  Translate Xcode Strings Catalog using Gemini service.
  anthropic               Translate Xcode Strings Catalog using Anthropic service.
  list_keys               List API keys stored in Keychain.
  delete_key              Delete an API keys stored in Keychain.
  print_key               Print an API key stored in Keychain to STDOUT.

  See 'strings_catalog_translate help <subcommand>' for detailed help.

这些子命令大多具有相同的参数,但有一些特定于平台的变体。

% ./.build/release/strings_catalog_translate gemini -h

OVERVIEW: Translate Xcode Strings Catalog using Gemini service.

USAGE: strings_catalog_translate gemini [--verbose] [--key <key>] [--available_languages] [--input-file <input-file>] [--output-file <output-file>] [--target-language <target-language>] [--model <model>] [--project-id <project-id>]

OPTIONS:
  -v, --verbose           Enable verbose output to STDOUT.
  -k, --key <key>         --key <key> 
                          1. **From Keychain**:
                              - Format: `key_id:[YOUR_KEY_ID]` (e.g., `key_id:key1`)
                              - The tool will search for `YOUR_KEY_ID` in the keychain.
                              - If not found, you will be prompted to enter the key.
                              - The entered key will be securely saved under `YOUR_KEY_ID` for future use.
                          2. **From Environment Variable**:
                              - Set a standard environment variable like `OPENAI_API_KEY`.
                              - Or specify a custom variable using the format `env:MY_API_KEY`.
                          3. **Direct Value**:
                              - Provide the API key as a plain string (e.g., `--key your-api-key`).
  --available_languages   List all available translation language codes supported by the service.
  -i, --input-file <input-file>
                          Path to the input Strings Catalog file. (default: Localizable.xcstrings)
  -o, --output-file <output-file>
                          Path to the output Strings Catalog file.
                              - This file will be overwritten if it exists.
                              - Use `-` to output to STDOUT. (default: Localizable.xcstrings)
  -t, --target-language <target-language>
                          Target language identifier (e.g., `de` for German). Case-insensitive.
  --model <model>         Specify the model to use. (default: gemini-1.5-flash)
  --project-id <project-id>
                          Specify a Project ID
  --version               Show the version.
  -h, --help              Show help information.

注意

  1. 只会处理未翻译的字符串。 那些已翻译或在 Strings Catalog 中标记为“不要翻译”的字符串将被跳过。
  2. 默认情况下,Localizable.xcstrings 文件将被修改。

API 密钥

保存在钥匙串中的 API 密钥可以使用 macOS 钥匙串应用程序进行管理,并搜索以 tools.xcode.translate_strings 为前缀的密钥。


构建

  1. 运行 swift build -c release 以在 ./.build/release 中构建 translate_strings 可执行文件。
  2. 将可执行文件复制到 $PATH 中的某个位置以便于访问。

包目标

该包有两个目标

  1. strings_catalog_translate 可执行命令行工具。
  2. TranslationService 可执行文件用于翻译服务的库。它指定了 DeepL、Anthropic 和 OpenAI 的协议和具体类型。