# gt: General Translation CLI tool: .strings
URL: https://generaltranslation.com/zh/docs/cli/reference/formats/dot-strings-files.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 使用 General Translation CLI 翻译 .strings 文件。.strings 文件格式的 API 参考。

CLI 可翻译 `.strings` 文件，即应用从每个区域设置各自的 `<locale>.lproj` 目录中加载的键/值文本表。翻译会保留每个条目的键、转义符和格式说明符，条目上方的注释会作为上下文传递给翻译引擎。

*注意：`.strings` 和 `.stringsdict` 属于 Apple 的旧版本地化布局。Xcode 的 String Catalog 迁移会将 `.lproj` 文件合并进单个 [`.xcstrings` 目录](/docs/cli/reference/formats/xcstrings-files)并删除原文件，因此一个项目只会采用其中一种布局。*

## 配置 [#config]

在 `files` 下添加一个 `dotStrings` 条目，并为其配置一个由 glob 模式组成的 `include` 数组。请将 `[locale]` 占位符放在 `.lproj` 目录名中，这样 CLI 才能找到源表，并将翻译保存到正确的路径。

```json title="gt.config.json"
{
  "defaultLocale": "en",
  "locales": ["es", "ja", "ar"],
  "files": {
    "dotStrings": {
      "include": [
        "Resources/[locale].lproj/Localizable.strings",
        "Resources/[locale].lproj/InfoPlist.strings"
      ]
    }
  }
}
```

使用此配置时，CLI 会读取 `Resources/en.lproj/` 中的表，并写入 `Resources/es.lproj/`、`Resources/ja.lproj/` 和 `Resources/ar.lproj/`。有关所有文件键，请参阅[配置参考](/docs/cli/reference/config#files)。

`dotStrings` 键需要 `gt` 2.18.1 或更高版本。

## 文件编码 [#encoding]

CLI 在读取 `.strings` 文件时会检测 UTF-8、UTF-16 和 UTF-32 字节顺序标记 (BOM) ，将解码后的文本发送给 API，然后以源文件的原始编码写入每份翻译。不带字节顺序标记的文件会按 UTF-8 处理。

如果源文件在上传后被移动或删除，CLI 会在存在翻译后的文件时沿用其编码。

<Callout type="info">
  **v2.20.0 变更：** CLI 现在会对解码后的文本计算哈希，并保留源文件编码。现有的 `.strings` 文件会获得新的 version ID，因为早期版本是对 base64 字节计算哈希的，所以即使文本没有变化，升级后它们也会再翻译一次。
</Callout>

## 输出行为 [#output]

当翻译后的表中没有任何条目时，CLI 会写入一个空的属性列表字典，而不是零字节文件，以便 Foundation 能够加载它。

## 每个区域设置对应多个表 [#tables]

大多数应用为每个区域设置提供不止一个表，因此仅列出 `Localizable.strings` 的配置会让其余内容被悄无声息地漏译。常见的两个表是：

* `Localizable.strings` —— 通过 `NSLocalizedString` 加载的界面文本
* `InfoPlist.strings` —— `Info.plist` 中面向系统的值，例如 `CFBundleDisplayName` 以及权限提示中显示的用途说明

在 `include` 中逐一列出每个表，或用单个 glob 模式一次性匹配全部：

```json title="gt.config.json"
{
  "files": {
    "dotStrings": {
      "include": ["Resources/[locale].lproj/*.strings"]
    }
  }
}
```

## 复数 [#plurals]

`.strings` 文件中每个键只能保存一个字符串，无法表达复数形式。Apple 将复数规则放在同一 `.lproj` 目录下配套的 `.stringsdict` 文件中，它对应一个独立的配置键。请参阅 [`.stringsdict`](/docs/cli/reference/formats/dot-stringsdict-files)，以便将其与你的表一同翻译。

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
