Communitygithub.com

Flutter アプリの国際化

Flutter プロジェクトに `flutter_localizations` と `intl` の依存関係を追加し、`pubspec.yaml` で `generate: true` を有効にし、`l10n.yaml` 設定ファイルを作成します。新しい Flutter プロジェクトでローカライゼーションのサポートを初期化する際に使用します。

Flutter アプリの国際化 とは?

このスキルは、Flutter アプリケーションで国際化 (i18n) とローカライゼーション (l10n) を開始するための完全なワークフローを提供します。`flutter_localizations` と `intl` パッケージを使用して、`.arb` ファイルで文字列を定義し、タイプセーフな `AppLocalizations` クラスを自動生成します。セットアップ手順として、依存関係の追加、コード生成の有効化、`l10n.yaml` 設定ファイルの作成、`MaterialApp` へのローカライゼーション設定の注入をカバーします。また、プレースホルダー、複数形、性別による選択などの高度なフォーマット機能もサポートし、Flutter アプリケーションの多言語対応を効率的に実装できます。

対応~Claude Code~Codex CLI~Cursor✓Gemini CLI
npx skills add https://github.com/dbillion/hermes-skills-public/tree/HEAD/skills-external/flutter-setup-localization

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

Flutter アプリケーションの国際化

目次

基本概念

Flutter は、flutter_localizations パッケージと intl パッケージを介して国際化 (i18n) とローカライゼーション (l10n) を処理します。標準的なアプローチでは、アプリリソースバンドル (.arb) ファイルを使用してローカライズされた文字列を定義し、それらをコンパイルして生成された AppLocalizations クラスに変換することで、ウィジェットツリー内でタイプセーフにアクセスできるようにします。

セットアップ手順

Flutter プロジェクトで国際化を初期化する際は、以下のチェックリストをコピーして追跡してください。

  • タスクの進捗
    • 1. pubspec.yaml に依存関係を追加する。
    • 2. generate フラグを有効にする。
    • 3. l10n.yaml 設定ファイルを作成する。
    • 4. MaterialApp または CupertinoApp を設定する。

1. 依存関係の追加

必要なローカライゼーションパッケージをプロジェクトに追加します。ターミナルで以下のコマンドを実行してください。

flutter pub add flutter_localizations --sdk=flutter
flutter pub add intl:any

pubspec.yaml の dependencies に以下が含まれていることを確認してください。

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: any

2. コード生成の有効化

pubspec.yaml を開き、flutter セクション内で generate フラグを有効にして、ローカライゼーションタスクを自動化します。

flutter:
  generate: true

3. 設定ファイルの作成

Flutter プロジェクトのルートディレクトリに l10n.yaml という名前の新しいファイルを作成します。入力ディレクトリ、テンプレートファイル、出力ファイルを定義します。

arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: true

4. アプリのエントリポイントの設定

生成されたローカライゼーションと flutter_localizations ライブラリを main.dart にインポートします。デリゲートとサポートされているロケールを MaterialApp または CupertinoApp に注入します。

import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; // synthetic-package が false の場合はパスを調整してください

// ... build メソッド内
return MaterialApp(
  localizationsDelegates: const [
    AppLocalizations.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  supportedLocales: const [
    Locale('en'), // 英語
    Locale('es'), // スペイン語
  ],
  home: const MyHomePage(),
);

実装ワークフロー

ローカライズされたコンテンツを追加または変更する際は、このワークフローに従ってください。

1. ARB ファイルの定義

  • 新しいコンテンツを作成する場合: テンプレートファイル (lib/l10n/app_en.arb) にベース文字列を追加します。説明も含めてください。
  • 既存のコンテンツを編集する場合: すべてのサポートされている .arb ファイル内のキーを見つけ、値を更新します。
{
  "helloWorld": "Hello World!",
  "@helloWorld": {
    "description": "The conventional newborn programmer greeting"
  }
}

他のロケールに対応するファイル (例: app_es.arb) を作成します。

{
  "helloWorld": "¡Hola Mundo!"
}

2. ローカライゼーションクラスの生成

以下のコマンドを実行してコード生成をトリガーします。

flutter pub get

フィードバックループ: バリデーターを実行 -> ターミナル出力で ARB 構文エラーを確認 -> 不足しているカンマやプレースホルダーの不一致を修正 -> flutter pub get を再実行します。

3. ローカライズされた文字列の利用

AppLocalizations.of(context) を使用して、ウィジェットツリー内でローカライズされた文字列にアクセスします。これを呼び出すウィジェットが MaterialApp の子孫であることを確認してください。

Text(AppLocalizations.of(context)!.helloWorld)

高度なフォーマット

動的データ、複数形、条件付き選択のためにプレースホルダーを使用します。

プレースホルダー

中括弧内にパラメータを定義し、メタデータオブジェクトでその型を指定します。

"hello": "Hello {userName}",
"@hello": {
  "description": "A message with a single parameter",
  "placeholders": {
    "userName": {
      "type": "String",
      "example": "Bob"
    }
  }
}

複数形

plural 構文を使用して、数量に基づく文字列のバリエーションを処理します。other ケースは必須です。

"nWombats": "{count, plural, =0{no wombats} =1{1 wombat} other{{count} wombats}}",
"@nWombats": {
  "description": "A plural message",
  "placeholders": {
    "count": {
      "type": "num",
      "format": "compact"
    }
  }
}

選択

select 構文を使用して、性別によるテキストなどの条件付き文字列を処理します。

"pronoun": "{gender, select, male{he} female{she} other{they}}",
"@pronoun": {
  "description": "A gendered message",
  "placeholders": {
    "gender": {
      "type": "String"
    }
  }
}

例

完全な l10n.yaml

arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: true
use-escaping: true

完全なウィジェット実装

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class GreetingWidget extends StatelessWidget {
  final String userName;
  final int notificationCount;

  const GreetingWidget({
    super.key, 
    required this.userName, 
    required this.notificationCount,
  });

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;

    return Column(
      children: [
        Text(l10n.hello(userName)),
        Text(l10n.nWombats(notificationCount)),
      ],
    );
  }
}

Individual skills in this repo

This repo contains 6 individual skills — each has its own dedicated page.

dbillion/hermes-skills-public

Edit an existing image based on a text prompt.

dbillion/hermes-skills-public

Expert in video composition, editing, and format conversion. Use when the user wants to generate high-quality video, overlay images on video, concatenate clips, create GIFs, or sync audio to video using mcp-avtool-go and mcp-veo-go.

dbillion/hermes-skills-public

All animation knowledge for HyperFrames — atomic motion rules, multi-phase scene blueprints, scene transitions, broader motion-design techniques, AND the seven runtime adapters (GSAP default, plus Lottie, Three.js, Anime.js, CSS keyframes, Web Animations API, TypeGPU). Use for any motion or animation task: pick 2-4 rules and compose, or load a blueprint, or look up runtime-specific API (e.g. GSAP eases / Lottie player / Three.js mixer). Also covers auditing an existing composition's choreography (animation map) and 24 named text-animation effects. HyperFrames-native: single paused timeline, seek-safe, deterministic.

dbillion/hermes-skills-public

Use the HyperFrames CLI development loop: init, add, catalog, capture, lint, check, snapshot, compare, grade-compare, preview, play, present, beats, keyframes, single or batch render, publish, cloud, cloudrun, feedback, lambda, doctor, browser, info, upgrade, skills, compositions, docs, benchmark, telemetry, transcribe, auth, tts, and remove-background. Also use when diagnosing build or render failures. validate, inspect, and layout are deprecated aliases; use check. Covers local, HeyGen-hosted cloud, AWS Lambda, and Google Cloud Run rendering.

dbillion/hermes-skills-public

The HyperFrames composition contract — build one renderable project. Use for composition structure, the `data-*` timing attributes, `class="clip"`, tracks, sub-compositions, variables, framework-owned media playback, deterministic-render rules, and validation. Also covers Tailwind projects and the STORYBOARD.md / SCRIPT.md plan formats. Read before writing composition HTML.

dbillion/hermes-skills-public

Non-animation creative direction for HyperFrames videos. Use for design spec (frame.md / design.md) handling, palettes, typography, narration, beat planning, audio-reactive visuals, composition patterns, and brand / style decisions. For atomic motion patterns and scene blueprints, use `hyperframes-animation`.

関連スキル