【Flutterで多言語対応】VSCODEのプラグインで楽々

Flutter Intlをインストール

file

初期化を行う

Ctrl+Shift+Pで以下入力

>Flutter intl: Initialize

初期化でいろいろ追加される

file

flutter_intl:
  enabled: true

多言語対応パッケージを入れる

dependencies:
   flutter_localizations: # 多言語対応
   sdk: flutter

日本語の言語ファイルを作る

Ctrl+Shift+Pで以下入力

> Flutter intl: Add locale

file

日本語ファイルが追加される

file

対応言語を設定

対応してるのには全部、入れないと使えない。

intl_en.arb

{
    "hello": "hello"
}

intl_ja.arb

{
    "hello": "こんにちは"
}

MaterialAppに設定を追加する

意味はよくわからん。

import 'package:flutter_localizations/flutter_localizations.dart';
import 'generated/l10n.dart';
return MaterialApp(
  localizationsDelegates: const [
    S.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  supportedLocales: S.delegate.supportedLocales,
  …
  …

言語の選択

同じくMaterialAppに追加する

locale: const Locale('ja'),

使い方

日本語にしていれば、こんにちはが返ってきます!

S.of(context).hello;

Flutter開発で知らないと損すること Flutter開発で知らないと損すること

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です