推送
$ 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 文件中配置。
推送示例:
推送:
源:
- 文件:./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:
列分隔符:";"
以下占位符和通配符操作符可以放在文件条目的路径中:
通配符
* 和 ** 是通配符操作符。单个星号 * 跳过路径中的任意文件夹。双星号 ** 与标准通配符操作符一样,用于递归地、非穷尽地匹配任意字符。
示例:
# 文件模式 ./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。
上传文件到指定区域
使用指定的 locale_id 将 en.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