JSON

Lingo.dev CLIによるJSONファイルのAI翻訳

JSONとは?

JSON(JavaScript Object Notation)は、人間が読み書きしやすく、機械が解析・生成しやすい軽量のデータ交換フォーマットです。設定ファイル、データストレージ、APIコミュニケーションなどによく使用されます。

例えば:

{
  "title": "Hello, world!",
  "description": "A simple demo app",
  "author": {
    "name": "John Doe"
  },
  "messages": [
    "Welcome to MyApp",
    "Hello, world!"
  ],
  "config": {
    "theme": {
      "primary": "Blue theme"
    }
  }
}

Lingo.dev CLIとは?

Lingo.dev CLIは、AIでアプリやコンテンツを翻訳するための無料のオープンソースCLIです。従来の翻訳管理ソフトウェアに代わるものとして設計されており、既存のパイプラインと統合できます。

詳細については、概要をご覧ください。

このガイドについて

このガイドでは、Lingo.dev CLIを使用してJSONファイルを翻訳する方法を説明します。

以下の方法を学びます:

  • ゼロからプロジェクトを作成する
  • 翻訳パイプラインを設定する
  • AIで翻訳を生成する

前提条件

Lingo.dev CLIを使用するには、Node.js v18以上がインストールされていることを確認してください:

❯ node -v
v22.17.0

ステップ 1. プロジェクトのセットアップ

プロジェクトのディレクトリにi18n.jsonファイルを作成します:

{
  "$schema": "https://lingo.dev/schema/i18n.json",
  "version": "1.10",
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {}
}

このファイルは、翻訳パイプラインの動作を定義し、どの言語間で翻訳するか、ローカライズ可能なコンテンツがファイルシステム上のどこに存在するかを指定します。

利用可能なプロパティの詳細については、i18n.jsonをご覧ください。

ステップ 2. ソースロケールの設定

_ソースロケール_は、コンテンツが最初に書かれた元の言語と地域です。ソースロケールを設定するには、i18n.jsonファイルのlocale.sourceプロパティを設定します:

{
  "$schema": "https://lingo.dev/schema/i18n.json",
  "version": "1.10",
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {}
}

ソースロケールはBCP 47言語タグとして提供する必要があります。

Lingo.dev CLIがサポートするロケールコードの完全なリストについては、サポートされているロケールコードをご覧ください。

ステップ 3. ターゲットロケールを設定する

_ターゲットロケール_とは、コンテンツを翻訳したい言語や地域のことです。ターゲットロケールを設定するには、i18n.jsonファイル内のlocale.targetsプロパティを設定します:

{
  "$schema": "https://lingo.dev/schema/i18n.json",
  "version": "1.10",
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {}
}

ステップ 4. ソースコンテンツを作成する

まだ作成していない場合は、翻訳するコンテンツを含む1つ以上のJSONファイルを作成します。これらのファイルは、パスのどこかにソースロケールを含むパスに配置する必要があります(例:ディレクトリ名としてen/や、ファイル名の一部としてmessages.en.jsonなど)。

ステップ 5. バケットを作成する

  1. i18n.jsonファイル内のbucketsオブジェクトに"json"オブジェクトを追加します:

    {
      "$schema": "https://lingo.dev/schema/i18n.json",
      "version": "1.10",
      "locale": {
        "source": "en",
        "targets": ["es"]
      },
      "buckets": {
        "json": {}
      }
    }
    
  2. "json"オブジェクト内で、1つ以上のincludeパターンの配列を定義します:

    {
      "$schema": "https://lingo.dev/schema/i18n.json",
      "version": "1.10",
      "locale": {
        "source": "en",
        "targets": ["es"]
      },
      "buckets": {
        "json": {
          "include": ["./[locale]/example.json"]
        }
      }
    }
    

    これらのパターンは、翻訳するファイルを定義します。

    パターン自体は:

    • 設定されたロケールのプレースホルダーとして[locale]を含む必要があります
    • ファイルパスを指定できます(例:"[locale]/config.json"
    • ワイルドカードプレースホルダーとしてアスタリスクを使用できます(例:"[locale]/*.json"

    再帰的なグロブパターン(例:**/*.json)はサポートされていません。

ステップ 6. LLMを設定する

Lingo.dev CLIは、AIでコンテンツを翻訳するために大規模言語モデル(LLM)を使用します。これらのモデルを使用するには、サポートされているプロバイダーからAPIキーが必要です。

可能な限り早く始めるには、毎月10,000トークンの無料使用量を提供する当社独自のホスティングプラットフォームLingo.dev Engineの使用をお勧めします:

  1. Lingo.devアカウントにサインアップします。

  2. 次のコマンドを実行します:

    npx lingo.dev@latest login
    

    これによりデフォルトのブラウザが開き、認証を求められます。

  3. 画面の指示に従います。

ステップ 7. 翻訳を生成する

i18n.jsonファイルが含まれているディレクトリで、次のコマンドを実行します:

npx lingo.dev@latest run

このコマンドは以下を実行します:

  1. i18n.jsonファイルを読み込みます。
  2. 翻訳が必要なファイルを見つけます。
  3. ファイルから翻訳可能なコンテンツを抽出します。
  4. 設定されたLLMを使用して抽出されたコンテンツを翻訳します。
  5. 翻訳されたコンテンツをファイルシステムに書き戻します。

翻訳が初めて生成されるとき、i18n.lockファイルが作成されます。このファイルは、どのコンテンツが翻訳されたかを追跡し、後続の実行で不要な再翻訳を防ぎます。

en/example.json

{
  "title": "Hello, world!",
  "description": "A simple demo app",
  "version": "1.0.0",
  "support_email": "[email protected]",
  "homepage": "https://lingo.dev",
  "deprecated": null,
  "empty": "",
  "emoji": "🚀",
  "author": {
    "name": "John Doe"
  },
  "contributors": [
    { "name": "Alice" },
    { "name": "Bob" }
  ],
  "messages": [
    "Welcome to MyApp",
    "Hello, world!"
  ],
  "config": {
    "theme": {
      "primary": "Blue theme"
    }
  },
  "mixed_array": [
    "Mixed content here",
    42,
    true,
    {
      "nested_message": "Nested text"
    }
  ],
  "locked_key_1": "This value is locked and should not be changed",
  "ignored_key_1": "This value is ignored and should not appear in target locales"
}

es/example.json

{
  "title": "¡Hola, mundo!",
  "description": "Una aplicación de demostración simple",
  "version": "1.0.0",
  "support_email": "[email protected]",
  "homepage": "https://lingo.dev",
  "deprecated": null,
  "empty": "",
  "emoji": "🚀",
  "author": {
    "name": "Juan Pérez"
  },
  "contributors": [
    { "name": "Alicia" },
    { "name": "Roberto" }
  ],
  "messages": [
    "Bienvenido a MyApp",
    "¡Hola, mundo!"
  ],
  "config": {
    "theme": {
      "primary": "Tema azul"
    }
  },
  "mixed_array": [
    "Contenido mixto aquí",
    42,
    true,
    {
      "nested_message": "Texto anidado"
    }
  ],
  "locked_key_1": "This value is locked and should not be changed"
}

i18n.json

{
  "version": "1.10",
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {
    "json": {
      "include": ["./[locale]/example.json"],
      "lockedKeys": ["locked_key_1"]
    }
  },
  "$schema": "https://lingo.dev/schema/i18n.json"
}

i18n.lock

version: 1
checksums:
  455da9346f4e772000927cd2ff5bb898:
    title: 0468579ef2fbc83c9d520c2f2f1c5059
    description: 49f8864eb0e53903f04532bf33e1e4fa
    version: 54a9e730e88fb16291b852274d433923
    support_email: 10627fcc465897af0f5e1bba042685f9
    emoji: b328c432cee108a87a92f05258b6a651
    author/name: febee8e9ab40b2fe5106d72675228d00
    contributors/0/name: e80d4063a32adaad7b0a82b0bcc10551
    contributors/1/name: b2bca2fa3c890618e56d07473f26ead3
    messages/0: d1c3a9f35e377554a4ccaa467ca26614
    messages/1: 0468579ef2fbc83c9d520c2f2f1c5059
    config/theme/primary: 7535a3779d6934ea8ecf18f5cb5b93fd
    mixed_array/0: 001b5b003d96c133534f5907abffdf77
    mixed_array/3/nested_message: 5f0782dfc5993e99890c0475bc295a30