幫助中心

使用 AI 自動化本地化

本地化自動化改變了您翻譯應用程式的方式。ai-l10n npm 套件將 AI 驅動的翻譯直接帶入您的開發工作流程,支援 JSON 和 Flutter ARB 檔案,並具備智能自動化功能。

AI-powered localization automation demonstration

AI 驅動的本地化自動化實例

為什麼要自動化本地化?

手動翻譯管理既耗時又容易出錯。自動化提供:

  • 利用 AI 驅動的自動化節省數小時的手動翻譯工作。
  • 確保所有翻譯的一致性,並具備上下文感知的 AI。
  • 將翻譯直接整合到您的 CI/CD 管道中。
  • 自動檢測和翻譯新字串,同時保留現有翻譯。
  • 支援 165 種以上語言,並具備上下文感知的翻譯質量。

AI 驅動的功能

ai-l10n 套件使用先進的 AI 提供生產級翻譯:

上下文感知翻譯

AI 理解您字串的上下文,保留佔位符、HTML 標籤和格式,同時將日期和數字調整為目標地區的格式。它智能地避免翻譯專有名稱、URL 和技術術語。

智能專案檢測

自動從您的專案結構(基於資料夾或檔案)檢測目標語言。如果語言已存在於您的專案中,則無需手動指定語言。

類型安全與格式保留

保留 JSON 數據類型——數字保持數字,布林值保持布林值,null 值保持不變。支援 i18next 複數形式,自動生成複雜複數化規則的後綴。

智能錯誤檢測與分塊

自動檢測並重試,如果佔位符或格式丟失。對於大型檔案,將內容拆分為可管理的塊,同時保持上下文。防止直接 AI 上傳常見的問題,因為超過 ~16,000 字符會導致內容丟失。

開始使用

安裝

使用 npm、yarn 或 pnpm 安裝 ai-l10n 套件:

npm install ai-l10n

配置 API 金鑰

獲取您的免費 API 金鑰並進行配置:

npx ai-l10n config --api-key YOUR_API_KEY

您的第一次翻譯

使用簡單的命令翻譯您的本地化檔案:

# Auto-detect target languages from project structure
npx ai-l10n translate path/to/en.json

# Specify target languages
npx ai-l10n translate path/to/en.json --languages es,fr,de

使用場景

命令行介面

使用 CLI 進行快速翻譯,並完全控制選項:

npx ai-l10n translate ./locales/en.json \
  --languages es,fr,de,ja,zh-CN \
  --plural \
  --verbose

程式化使用

將翻譯直接整合到您的 Node.js 應用程式中:

import { AiTranslator } from 'ai-l10n';

const translator = new AiTranslator();

// Basic translation
const result = await translator.translate({
  sourceFile: './locales/en.json',
  targetLanguages: ['es', 'fr', 'de'],
});

console.log(`Translated to ${result.results.length} languages`);
console.log(`Used ${result.totalCharsUsed} characters`);

CI/CD 整合

無縫地將 AI 驅動的翻譯整合到您的 CI/CD 流程中,使用我們的 ai-l10n GitHub Action

創建一個配置文件:

// example ai-l10n.config.json
[
  {
    "sourceFile": "./locales/en/common.json",
    "targetLanguages": ["es", "fr", "de"],
    "translateOnlyNewStrings": true
  },
  {
    "sourceFile": "./locales/en/common.json",
    "targetLanguages": ["pl", "ar"],
    "generatePluralForms": true,
    "translateOnlyNewStrings": true
  }
]

添加到您的 GitHub Actions 工作流程:

name: Auto-translate i18n files

on:
  push:
    branches:
      - main
    paths:
      - 'locales/en/**'
      - 'ai-l10n.config.json'

permissions:
  contents: write

jobs:
  translate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: l10n-dev/ai-l10n@v1
        with:
          api-key: ${{ secrets.L10N_API_KEY }}
          config-file: 'ai-l10n.config.json'
          pull-request: false

使用 ai-l10n 批次命令的 GitLab CI 配置:

# .gitlab-ci.yml
translate:
  stage: deploy
  image: node:20-alpine
  only:
    - main
  changes:
    - locales/en/**/*
    - ai-l10n.config.json
  script:
    - npm install -g ai-l10n
    - npx ai-l10n batch ai-l10n.config.json
  variables:
    L10N_API_KEY: $L10N_API_KEY

使用 ai-l10n 批次命令的 Jenkins 流水線配置:

// Jenkinsfile
pipeline {
  agent any

  stages {
    stage('Translate') {
      when {
        changeset "locales/en/**"
      }
      steps {
        script {
          sh 'npm install -g ai-l10n'
          withCredentials([string(
            credentialsId: 'l10n-api-key',
            variable: 'L10N_API_KEY'
          )]) {
            sh 'npx ai-l10n batch ai-l10n.config.json'
          }
        }
      }
    }
  }
}

進階功能

增量更新

僅翻譯新鍵,同時保留現有翻譯:

# Only translate new keys, preserve existing translations
npx ai-l10n translate path/to/en.json --update

批量翻譯

一次翻譯多個檔案,並使用配置檔:

// example translate-config.json
[
  {
    "sourceFile": "./locales/en/common.json",
    "targetLanguages": ["pl", "ru", "ar"],
    "generatePluralForms": true,
    "translateOnlyNewStrings": true
  },
  {
    "sourceFile": "./locales/en/admin.json",
    "targetLanguages": ["pl", "ru", "ar", "de"]
  }
]
npx ai-l10n batch translate-config.json

i18next 複數形式

自動生成所有所需的複數形式字串,並為具有複雜複數化規則的語言(如俄語、阿拉伯語或波蘭語)提供正確的後綴:

npx ai-l10n translate ./locales/en.json \
  --languages ru,ar,pl \
  --plural

Flutter ARB 支援

完全支援 Flutter 應用程式資源包檔案,並自動更新元數據:

npx ai-l10n translate ./lib/l10n/app_en_US.arb \
  --languages es_ES,fr_FR,de

語言支援

l10n.dev 支援 165 種以上語言,並具備不同的熟練程度:

  • 強 (12 種語言): 英語、西班牙語、法語、德語、中文、俄語、葡萄牙語、義大利語、日語、韓語、阿拉伯語、印地語
  • 高 (53 種語言): 大多數歐洲和亞洲語言,包括荷蘭語、瑞典語、波蘭語、土耳其語、越南語、泰語等
  • 中等 (100+ 種語言): 各種世界語言

專案結構檢測

ai-l10n 自動檢測您的專案結構並相應生成翻譯:

基於資料夾的結構

按語言資料夾組織翻譯:

locales/
  en/
    common.json
    errors.json
  es/              # Auto-detected
    common.json
    errors.json
  fr-FR/           # Auto-detected
    common.json

基於檔案的結構

為每種語言使用單獨的檔案:

locales/
  en.json          # Source
  es.json          # Auto-detected
  fr-FR.json       # Auto-detected
  zh-Hans-CN.json  # Auto-detected

最佳實踐

  • 使用增量更新: 使用 --update 標誌僅翻譯新字串,保留現有翻譯並節省字元。
  • 與 CI/CD 整合: 在每次提交源語言檔案時自動化翻譯,確保翻譯始終保持最新。
  • 安全存儲 API 金鑰: 在 CI/CD 環境中使用環境變數或秘密管理來存儲 API 金鑰。
  • 檢查過濾的內容: 檢查 .filtered 檔案,查看因內容政策違規而被排除的字串。
  • 使用批量配置: 對於多個檔案,使用批量翻譯配置從單個命令管理所有翻譯。

準備好使用 AI 簡化您的本地化工作流程了嗎?