Android XMLリソース

Lingo.dev CLIによるAndroid XMLリソースファイルのAI翻訳

Android XMLリソースとは?

Androidではローカライゼーション用の文字列リソースを保存するためにXMLベースのリソースファイルを使用しています。これらのファイルは通常、res/values/ディレクトリにロケール固有のサブディレクトリ(例:スペイン語の場合はres/values-es/)に保存されます。

例:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MyApp</string>
    <string name="welcome_message">Hello, world!</string>
    <string-array name="color_names">
        <item>Red</item>
        <item>Green</item>
    </string-array>
    <plurals name="notification_count">
        <item quantity="one">%d new message</item>
        <item quantity="other">%d new messages</item>
    </plurals>
</resources>

注意: translatable="false"とマークされた文字列は翻訳から除外され、ソースロケールファイルにのみ表示されます。

Lingo.dev CLIとは?

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

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

このガイドについて

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

以下の方法を学びます:

  • ゼロからプロジェクトを作成する
  • 翻訳パイプラインを設定する
  • 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つ以上のAndroid XMLリソースファイルを作成します。

Android XMLリソースファイルの場合、以下の要件が適用されます:

  • ファイルは<resources>ルート要素を持つ有効なXMLである必要があります。
  • 文字列リソースは<string name="key">value</string>タグで定義されます。
  • 文字列配列は<string-array name="key"><item>value</item></string-array>構造を使用します。
  • 複数形は<plurals name="key"><item quantity="one|other">value</item></plurals>構造を使用します。
  • translatable="false"とマークされた要素は翻訳されません。
  • ブール値(<bool>)および整数(<integer>)リソースタイプをサポートしています。
  • CDATAセクションとHTML実体は保持されます。

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

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

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

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

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

    パターン自体:

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

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

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

Lingo.dev CLIは大規模言語モデル(LLM)を使用してAIでコンテンツを翻訳します。これらのモデルを使用するには、サポートされているプロバイダーから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.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MyApp</string>
    <string name="welcome_message">Hello, world!</string>
    <string name="button_text">Get Started</string>

    <string name="api_endpoint" translatable="false">https://api.example.com</string>

    <string-array name="color_names">
        <item>Red</item>
        <item>Green</item>
        <item>Blue</item>
    </string-array>

    <plurals name="notification_count">
        <item quantity="one">%d new message</item>
        <item quantity="other">%d new messages</item>
    </plurals>
</resources>

es/example.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MyApp</string>
    <string name="welcome_message">¡Hola, mundo!</string>
    <string name="button_text">Comenzar</string>

    <string name="api_endpoint" translatable="false">https://api.example.com</string>

    <string-array name="color_names">
        <item>Rojo</item>
        <item>Verde</item>
        <item>Azul</item>
    </string-array>

    <plurals name="notification_count">
        <item quantity="one">%d mensaje nuevo</item>
        <item quantity="other">%d mensajes nuevos</item>
    </plurals>
</resources>

i18n.json

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

i18n.lock

version: 1
checksums:
  ec06a6ebae97ffd5f7afc99d9a8f051b:
    app_name: 7dc70110429d46e3685f385bd2cc941c
    welcome_message: 0468579ef2fbc83c9d520c2f2f1c5059
    button_text: 1d5f030c4ec9c869e647ae060518b948
    html_snippet: f060191b1af70b3848106a4df91f43cd
    apostrophe_example: 997099339b144b06266f8da411de8d93
    cdata_example: ba876d1379f854628eaebf67ea330ccc
    color_names/0: bace0083b78cdb188523bc4abc7b55c6
    color_names/1: 482ff383a4258357ba404f283682471d
    color_names/2: a5cf034b2d370a976119335cd99f4217
    mixed_items/0: 9278f79dfb062c6c04f6395108907816
    mixed_items/1: 9823a57cbe6e6e84c1d025ce24a1eec4
    notification_count/one: fe0aceb70f334c52a87937c36898a1d0
    notification_count/other: 13acfd95b16962ebe1f67dcd343513e1