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

或点击选择文件

目标语言

无需注册即时估价

常见问题