CLI

修改 CLI 配置文件 (Strings)

文本由 Phrase Language AI 从英语机器翻译而得。

推送和拉取

推送

$ phrase push 

Push 命令上传本地项目目录中的文件。使用 .phrase.yml 文件指定要上传的文件并设置其他参数。<tag> 占位符也可用于根据键的标签将其下载到单独的文件中。

拉取

$ phrase pull 

push 命令类似,但 globbing 除外。使用占位符时,尽量使用 <locale_name>。要拉取带有特定标签的文件键,请使用 tags 参数:

phrase:
  pull:
    targets:
    - file: path/to/file/<locale_name>.yml
      params:
        tags:tag_name

要使用 <tag> 占位符,必须在 params 部分中列出所需的标签。

参数

Pushpull 命令可以在 .phrase.yml 文件中配置。

注释

上传 API 端点的所有选项都支持推送命令。

本地化下载 API 端点的所有选项都支持拉取命令。

推送示例:

推送:
  源:
  - 文件:./locales/en.json
    params:
      update_translations: true 

拉取示例:

pull:
  targets:
  - 文件:"./locales/example.yml"
    params:
      include_unverified_translations: true

格式选项

根据文件格式,格式选项可以应用于参数部分。格式选项可以应用于上传、下载或两者皆有。

格式示例:

params:
  format_options:
    转换占位符:true

一些文件格式对文件语法有更大的控制权:

phrase:
  pull:
    targets:
    - 文件:file.xml
      params:
        format_options:
          转换占位符:true

  推送:
    源:
    - file: file.csv
      params:
        format_options:
          列分隔符:";"

占位符和通配符

以下占位符和通配符操作符可以放在文件条目的路径中:

  • <locale_name>

    唯一的区域名称。

  • <locale_code>

    符合 RFC 5646 的区域标识符。区域代码不必是唯一的,因此可以存在多个具有不同名称但代码相同的区域。

  • <tag>

    使用标签对键进行分组。标签可用于保持原始文件结构

通配符

*** 是通配符操作符。单个星号 * 跳过路径中的任意文件夹。双星号 ** 与标准通配符操作符一样,用于递归地、非穷尽地匹配任意字符。

示例:

# 文件模式

./abc/**/*.yml
# 系统上的一些文件

./abc/defg/en.yml

./abc/es.yml

./fr.yml
# 选择

./abc/defg/en.yml

./abc/es.yml

使用 pull 命令下载文件时,需提供明确的文件模式,如 ./abc/defg/<locale_name>.yml,而不是./**/*.yml

等待上传

所有上传都是异步处理的。要等待上传,需应用 --wait 标记。push 将等待每个文件上传,并返回是否成功或失败。

用例

上传文件到指定区域

使用指定的 locale_iden.json 文件上传到 ./locales/ 目录(区域必须已经存在)。

Update_translations 设置为 false,仅导入新键和翻译。如果设置为 true,客户端还将导入对现有翻译的本地更改,覆盖任何已存在的内容。

phrase:
  access_token: ACCESS_TOKEN
  project_id: PROJECT_ID
  file_format: nested_json

  推送:
    源:
    - 文件:./locales/en.json
      params:
        update_translations: false
        locale_id: YOUR_LOCALE_ID # 区域必须在远程存在

注释

导入的翻译条目无法删除。如果需要,可通过相关的 API 端点 将翻译更新为 empty 值。

Ruby on Rails:上传指定文件类型

上传位于 ./config/locales/ 中以 .yml 结尾的所有文件。Ruby on Rails YAML 文件包含区域信息,因此无需明确指定区域。Update_translations 设置为 true,因此所有对翻译的更改都将在导入时覆盖现有数据。

phrase:
  推送:
    源:
    - file: ./config/locales/*.yml
      params:
        update_translations: true
        file_format: yml

匹配 iOS 字符串

将所有 iOS 字符串文件与 .lproj 文件夹中名为 Localizable.strings 的文件匹配。

<locale_code> 是指 / 之后和 .lproj 之前的所有内容,用于创建和识别区域。

无需 update_translations 参数,效果与将其设置为 false 相同。

phrase:
  推送:
    源:
    - file: "./<locale_code>.lproj/Localizable.strings"
      params:
        file_format: strings

为一个本地化项目维护多个字符串项目

编辑配置文件,通过将翻译拆分为更小的类别来限制项目的范围。

框架配置示例

根据需求修改配置文件,然后将其提交到源代码管理或版本控制系统中。

Rails

phrase:
  access_token: ACCESS_TOKEN
  project_id: PROJECT_ID
  file_format: "yml"

  推送:
    源:
    - file: "./config/locales/*.yml"

  pull:
    targets:
    - file: "./config/locales/<locale_name>.yml"

iOS 字符串

phrase:

  access_token: ACCESS_TOKEN
  project_id: PROJECT_ID
  file_format: "strings"

  推送:
    源:
    - file: "./<locale_code>.lproj/Localizable.strings"

  pull:
    targets:
    - file: "./<locale_code>.lproj/Localizable.strings"
    - file: "./<locale_code>.lproj/Localizable.stringsdict"
      params:
        #file_format 可以被覆盖
        file_format: "stringsdict"

Android XML

Android 不使用标准的 ISO 语言代码作为文件模式。在 .phrase.yml 中指定所需的模式。

不必为每个区域定义单独的拉取目标,而是使用全局 locale_mapping 设置,并结合 <locale_name> 占位符。CLI 将使用映射中对应区域的自定义名称,并对所有其他语言回退到默认的 Phrase 区域代码。

示例:

phrase:
  access_token: ACCESS_TOKEN
  project_id: PROJECT_ID
  file_format: "xml"

  # 将 Phrase 区域映射到 Android 特定的目录名称
  locale_mapping:
    en-US: values
    de-DE: values-de-rDE
    fr-FR: values-fr

  推送:
    源:
      # 源文件是默认语言,映射到 'values'
      - file: ./app/src/main/res/values/strings.xml
        params:
          locale_id: en-US # 必须与 Phrase 中的源区域匹配

  pull:
    targets:
      # 使用 <locale_name> 占位符,该占位符将被映射替换
      - file: ./app/src/main/res/<locale_name>/strings.xml

显式设置区域示例

以英语为默认语言、德语区域和奥地利德语区域为例:

phrase:
  access_token: ACCESS_TOKEN
  project_id: PROJECT_ID
  file_format: xml
  
  pull:
    targets:
    - file: ./app/src/main/res/values/strings.xml
      params:
        file_format: xml
        # 英语的唯一区域 ID
        locale_id: LOCALE_ID
    - file: ./app/src/main/res/values-de/strings.xml
      params:
        file_format: xml
        # 德语的唯一区域 ID
        locale_id: LOCALE_ID
    - file: ./app/src/main/res/values-de-rAU/strings.xml
      params:
        file_format: xml
        # 奥地利的唯一区域 ID
        locale_id: LOCALE_ID
  推送:
    源:
    - file: ./app/src/main/res/values/strings.xml
      params:
        file_format: xml
        locale_id: LOCALE_ID
    - file: ./app/src/main/res/values-de/strings.xml
      params:
        file_format: xml
        locale_id: LOCALE_ID
    - file: ./app/src/main/res/values-de-rAU/strings.xml
      params:
        file_format: xml
        locale_id: LOCALE_ID

多平台示例

自动推送和拉取多个目标的翻译,使用一个配置。 

phrase:
  access_token: ACCESS_TOKEN
  project_id: PROJECT_ID
  file_format: nested_json

  推送:
    源:
      # Web JSON
      - file: "apps/web/src/assets/i18n/.json"
        params:
          file_format: nested_json
          locale_id: LOCALE_ID
      # 安卓 XML
      - file: "apps/android/app/src/main/res/values-/strings.xml"
        params:
          file_format: android
          locale_id: LOCALE_ID
      # iOS 字符串
      - file: "apps/ios/.lproj/Localizable.strings"
        params:
          file_format: strings
          locale_id: LOCALE_ID

  pull:
    targets:
      - file: "apps/web/src/assets/i18n/.json"
        params:
          file_format: nested_json
          locale_id: LOCALE_ID
      - file: "apps/android/app/src/main/res/values-/strings.xml"
        params:
          file_format: android
          locale_id: LOCALE_ID
      - file: "apps/ios/.lproj/Localizable.strings"
        params:
          file_format: strings
          locale_id: LOCALE_ID
这篇文章有帮助吗?

Sorry about that! In what way was it not helpful?

The article didn’t address my problem.
I couldn’t understand the article.
The feature doesn’t do what I need.
Other reason.

Note that feedback is provided anonymously so we aren't able to reply to questions.
If you'd like to ask a question, submit a request to our Support team.
Thank you for your feedback.