CLI

修改 CLI 配置文件 (Strings)

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

小贴士

有关更多详细信息,请参阅 Developer Hub

推送和拉取

推送

$ 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 部分中列出所需的标签。

参数

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

push 命令支持 uploads API 端点的所有选项。

pull 命令支持 locales download API 端点的所有选项。

Push 示例:

push:
  sources:
  - file: ./locales/en.json
    params:
      update_translations: true 

Pull 示例:

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

格式选项

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

格式示例:

params:
  format_options:
    convert_placeholder: true

一些文件格式对文件语法有更大的掌控度:

phrase:
  pull:
    targets:
    - file: file.xml
      params:
        format_options:
          convert_placeholder: true

  push:
    sources:
    - file: file.csv
      params:
        format_options:
          column_separator: ";"

占位符和通配符

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

  • <locale_name>

    唯一的区域名称。

  • <locale_code>

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

  • <tag>

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

通配符

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

示例:

# a file pattern

./abc/**/*.yml
# with a few files on your system

./abc/defg/en.yml

./abc/es.yml

./fr.yml
# selects

./abc/defg/en.yml

./abc/es.yml

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

等待上传

所有上传都是异步处理的。要等待上传,需应用 --wait 标记。在上传过程中,会等待每个文件上传完成,并返回是否成功。

用例

上传文件到指定区域

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

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

phrase:
  access_token: ACCESS_TOKEN
  project_id: PROJECT_ID
  file_format: nested_json

  push:
    sources:
    - file: ./locales/en.json
      params:
        update_translations: false
        locale_id: YOUR_LOCALE_ID # the locale must exist remotely

注释

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

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

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

phrase:
  push:
    sources:
    - file: ./config/locales/*.yml
      params:
        update_translations: true
        file_format: yml

匹配 iOS 字符串

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

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

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

phrase:
  push:
    sources:
    - file: "./<locale_code>.lproj/Localizable.strings"
      params:
        file_format: strings

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

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

框架配置示例

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

Rails

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

  push:
    sources:
    - 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"

  push:
    sources:
    - file: "./<locale_code>.lproj/Localizable.strings"

  pull:
    targets:
    - file: "./<locale_code>.lproj/Localizable.strings"
    - file: "./<locale_code>.lproj/Localizable.stringsdict"
      params:
        #file_format can be overwritten
        file_format: "stringsdict"

Android XML

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

无需为每个区域定义单独的 pull 译文,而是使用全局 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

  push:
    sources:
      # 原文/源语文件是默认语言,映射到 '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
        # Unique locale id for English
        locale_id: LOCALE_ID
    - file: ./app/src/main/res/values-de/strings.xml
      params:
        file_format: xml
        # Unique locale id for German
        locale_id: LOCALE_ID
    - file: ./app/src/main/res/values-de-rAU/strings.xml
      params:
        file_format: xml
        # Unique locale id for Austrian
        locale_id: LOCALE_ID
  push:
    sources:
    - 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

  push:
    sources:
      # Web JSON
      - file: \"apps/web/src/assets/i18n/.json\"
        params:
          file_format: nested_json
          locale_id: LOCALE_ID
      # Android 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.