Skip to main content

i18n Converter

在 32 種以上 i18n 檔案格式之間轉換,包括 Android XML、iOS Strings、XLIFF、PO、JSON、YAML 等。盡可能無損轉換,無法無損時會明確發出數據丟失警告。

問題

每個平台都使用不同的翻譯格式。Android 使用 strings.xml,iOS 使用 .strings 和 .stringsdict,網頁應用程式使用 JSON 或 YAML,譯員和 TMS 平台則需要 XLIFF 或 PO。要跨平台共享字串時,你只能在格式之間複製粘貼、編寫一次性腳本,或手動重組檔案。這不僅繁瑣且容易出錯,每出現一種新格式還會再次失效。

一個同時包含移動端和網頁端的項目很容易涉及 5 種以上翻譯檔案格式。手動保持同步注定難以為繼。

解決方案

i18n-convert 是一個 CLI 工具,可在 32 種以上 i18n 檔案格式之間轉換。它會自動檢測輸入格式、盡可能保留結構,並在轉換會丟失數據時發出警告,例如 CSV 無法保留複數。

Terminal
# 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 從源程式碼構建。

Terminal
npm install -g @i18n-agent/i18n-convert
npm 包包含適用於 macOS、Linux 和 Windows 的預構建二進制檔案,無需 Rust 工具鏈。

常見轉換

大多數團隊會反復使用少數幾種轉換:JSON 轉 Android XML 用於移動端、PO 轉 JSON 用於網頁端、XLIFF 往返轉換用於交接給譯員。以下是一些常見模式。

Batch conversion
# 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 種以上格式。

移動端

FormatExtensionFlag
Android XML.xml--to android-xml
iOS Strings.strings--to ios-strings
iOS Stringsdict.stringsdict--to ios-stringsdict
Flutter ARB.arb--to arb

網頁端

FormatExtensionFlag
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

本地化標準

FormatExtensionFlag
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

數據交換

FormatExtensionFlag
CSV.csv--to csv
TSV.tsv--to tsv
Excel.xlsx--to xlsx

數據丟失警告

格式並不等價。XLIFF 支援注釋、上下文和複數形式,CSV 則全都不支援。從資訊豐富的格式轉換為簡單格式時,i18n-convert 會準確說明將丟失哪些內容。

Terminal
$ 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-run
在生產環境中轉換為更簡單的格式前,請務必使用 --dry-run。它會顯示所有警告,但不會寫入任何檔案。

CI 整合

在 CI/CD 管線中自動同步格式。源翻譯檔案變化時,自動重新產生所有派生格式,讓移動端、網頁端和譯員檔案保持同步。

.github/workflows/format-sync.yml
# .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
在 CI 管線中結合使用 i18n-convert 和 i18n-validate。先轉換格式,再驗證所有目標檔案是否完整且格式正確。

立即試用 i18n Agent

將翻譯檔案拖放到此處

JSON, YAML, PO, XML, CSV, Markdown, Properties

或點擊選擇檔案

目標語言

無需註冊即時估價

常見問題