i18n Converter
在 32 種以上 i18n 檔案格式之間轉換,包括 Android XML、iOS Strings、XLIFF、PO、JSON、YAML 等。盡可能無損轉換,無法無損時會明確發出資料丟失警告。
問題
每個平台都使用不同的翻譯格式。Android 使用 strings.xml,iOS 使用 .strings 和 .stringsdict,網頁應用程式使用 JSON 或 YAML,譯員和 TMS 平台則需要 XLIFF 或 PO。要跨平台共享字串時,您只能在格式之間複製粘貼、編寫一次性腳本,或手動重組檔案。這不僅繁瑣且容易出錯,每出現一種新格式還會再次失效。
解決方案
i18n-convert 是一個 CLI 工具,可在 32 種以上 i18n 檔案格式之間轉換。它會自動偵測輸入格式、盡可能保留結構,並在轉換會丟失資料時發出警告,例如 CSV 無法保留複數。
# Auto-detect input format, convert to Android XML
i18n-convert en.json --to android-xml -o strings.xml
# iOS Strings to Gettext PO
i18n-convert Localizable.strings --to po -o messages.po
# XLIFF to Rails YAML
i18n-convert translations.xliff --to yaml-rails -o en.yml
# Pipe to stdout
i18n-convert messages.po --to json自動偵測
根據檔案副檔名和內容自動偵測輸入格式,無需提供標誌。
盡可能無損
目標格式支援時,會保留元資料、注釋和複數形式。
資料丟失警告
如果轉換會丟失資訊,工具會在寫入任何檔案前明確發出警告。
安裝
可透過 npm 輕鬆安裝,也可在 macOS 上使用 Homebrew,或透過 Cargo 從源程式碼建置。
npm install -g @i18n-agent/i18n-convert常見轉換
大多數團隊會反復使用少數幾種轉換:JSON 轉 Android XML 用於移動端、PO 轉 JSON 用於網頁端、XLIFF 往返轉換用於交接給譯員。以下是一些常見模式。
# Convert all PO files to JSON
for f in locales/*.po; do
i18n-convert "$f" --to json -o "${f%.po}.json"
done
# Convert entire directory
i18n-convert locales/ --to json --out-dir locales-json/支援的格式
i18n-convert 支援移動端、網頁端、在地化標準和資料交換類別中的 32 種以上格式。
移動端
| Format | Extension | Flag |
|---|---|---|
| Android XML | .xml | --to android-xml |
| iOS Strings | .strings | --to ios-strings |
| iOS Stringsdict | .stringsdict | --to ios-stringsdict |
| Flutter ARB | .arb | --to arb |
網頁端
| Format | Extension | Flag |
|---|---|---|
| JSON (flat) | .json | --to json |
| JSON (nested) | .json | --to json-nested |
| YAML | .yml | --to yaml |
| YAML (Rails) | .yml | --to yaml-rails |
| Properties | .properties | --to properties |
在地化標準
| Format | Extension | Flag |
|---|---|---|
| XLIFF 1.2 | .xlf | --to xliff |
| XLIFF 2.0 | .xlf | --to xliff-2 |
| Gettext PO | .po | --to po |
| Gettext POT | .pot | --to pot |
| TMX | .tmx | --to tmx |
| TBX | .tbx | --to tbx |
資料交換
| Format | Extension | Flag |
|---|---|---|
| CSV | .csv | --to csv |
| TSV | .tsv | --to tsv |
| Excel | .xlsx | --to xlsx |
資料丟失警告
格式並不等價。XLIFF 支援注釋、上下文和複數形式,CSV 則全都不支援。從資訊豐富的格式轉換為簡單格式時,i18n-convert 會準確說明將丟失哪些內容。
$ i18n-convert plurals.po --to csv
Data loss warnings:
[ERROR] 3 entries use plurals (not supported by CSV)
Proceed? [y/N]
# Skip prompts
i18n-convert plurals.po --to csv --force
# Preview warnings without writing
i18n-convert plurals.po --to csv --dry-runCI 整合
在 CI/CD 管線中自動同步格式。源翻譯檔案變化時,自動重新產生所有派生格式,讓移動端、網頁端和譯員檔案保持同步。
# .github/workflows/format-sync.yml
name: Sync Translation Formats
on:
push:
branches: [main]
paths: ['locales/en.json']
jobs:
sync-formats:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install i18n-convert
run: npm install -g @i18n-agent/i18n-convert
- name: Convert JSON to Android XML
run: i18n-convert locales/en.json --to android-xml -o android/res/values/strings.xml
- name: Convert JSON to iOS Strings
run: i18n-convert locales/en.json --to ios-strings -o ios/en.lproj/Localizable.strings
- name: Commit synced formats
run: |
git config user.name "github-actions"
git config user.email "[email protected]"
git add android/ ios/
git diff --cached --quiet || git commit -m "chore: sync translation formats"
git push立即試用 i18n Agent
將翻譯檔案拖放到此處
JSON, YAML, PO, XML, CSV, Markdown, Properties
或點擊選擇檔案
目標語言