Android XMLリソース

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

Android XMLリソースとは?

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

例:

<?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"

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

ステップ6. LLMの設定

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

できるだけ早く開始するために、Lingo.dev Engineの使用を推奨します。これは当社がホストするプラットフォームで、毎月10,000トークンの無料利用枠を提供しています:

  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