Contents
How internationalization in Flutter works
This section covers the technical details of how localizations work in Flutter. If you’re planning on supporting your own set of localized messages, the following content would be helpful. Otherwise, you can skip this section.
このセクションでは、Flutterでローカリゼーションがどのように機能するかについての技術的な詳細について説明します。独自のローカライズされたメッセージのセットをサポートすることを計画している場合は、次のコンテンツが役立ちます。それ以外の場合は、このセクションをスキップできます。
Loading and retrieving localized values
The Localizations
widget is used to load and lookup objects that contain collections of localized values.
Localizations
ウィジェットは、ローカライズされた値の集合が含まれているオブジェクトの読み取りとルックアップに使用されます。
Apps refer to these objects with Localizations.of(context,type)
.
アプリはこれらのオブジェクトをLocalizations.of(context,type)
で参照します。
If the device’s locale changes, the Localizations
widget automatically loads values for the new locale and then rebuilds widgets that used it.
デバイスのロケールが変更された場合、Localizations
ウィジェットは新しいロケールの値を自動的にロードし、それを使用しているウィジェットを再構築(リビルド)します。
This happens because Localizations
works like an InheritedWidget
.
LocalizationsウィジェットはInheritedWidgetのように機能するためこのようなことが起こります。
When a build function refers to an inherited widget, an implicit dependency on the inherited widget is created.
buildメソッドがInheritedWidgetを参照している時、暗黙的なInheritedWidgetへの依存が作られます。
When an inherited widget changes (when the Localizations
widget’s locale changes), its dependent contexts are rebuilt.
InheritedWidgetが変更される時(Localizationsウィジェットのロケールが変更される時)、それに依存しているcontexts(に紐づくウィジェット)はリビルドされます。
Localized values are loaded by the Localizations
widget’s list of LocalizationsDelegate
s.
ローカライズされた値は、Localizations
ウィジェットのLocalizationsDelegate
のリストによってロードされます。
Each delegate must define an asynchronous load()
method that produces an object that encapsulates a collection of localized values. Typically these objects define one method per localized value.
各デリゲートは 、ローカライズされた値のコレクションをカプセル化するオブジェクトを生成する非同期メソッドload()
を定義する必要があります。通常、これらのオブジェクトは、ローカライズされた値ごとに1つのメソッドを定義します。
In a large app, different modules or packages might be bundled with their own localizations.
大規模なアプリでは、別のモジュールまたはパッケージが独自のローカリゼーションにバンドルされている場合があります。
That’s why the Localizations widget manages a table of objects, one per LocalizationsDelegate.
そのため、Localizationsウィジェットは、LocalizationsDelegateごとに1つずつオブジェクトのテーブルを管理します。
To retrieve the object produced by one of the LocalizationsDelegate
’s load
methods, you specify a BuildContext
and the object’s type.
LocalizationsDelegate
の load
メソッドの1つによって生成されたオブジェクトを取得するには、BuildContext
とオブジェクトのタイプを指定します。
For example, the localized strings for the Material Components widgets are defined by the MaterialLocalizations
class.
たとえば、マテリアルコンポーネントウィジェットのローカライズされた文字列は、MaterialLocalizations
クラスによって定義されます。
Instances of this class are created by a LocalizationDelegate
provided by the MaterialApp
class.
このクラスのインスタンスはMaterialAppクラスのLocalizationDelegateによって生成されます。
They can be retrieved with Localizations.of()
:
MaterialLocalizationsはLocalizations.of()によって取得できます。
Localizations.of<MaterialLocalizations>(context, MaterialLocalizations);
This particular Localizations.of()
expression is used frequently, so the MaterialLocalizations
class provides a convenient shorthand:
この特定のLocalizations.of()
式は頻繁に使用されるため、MaterialLocalizations
クラスは下記の省略形を提供しています。
static MaterialLocalizations of(BuildContext context) {
return Localizations.of<MaterialLocalizations>(context, MaterialLocalizations);
}
/// References to the localized values defined by MaterialLocalizations
/// are typically written like this:
///MaterialLocalizationsに定義されているlocalized valuesを参照するために
///下記のように書くことができます。
tooltip: MaterialLocalizations.of(context).backButtonTooltip,
Defining a class for the app’s localized resources
Putting together an internationalized Flutter app usually starts with the class that encapsulates the app’s localized values.
国際化されたFlutterアプリをまとめるには、通常、アプリのローカライズされた値をカプセル化するクラスから始めます。
The example that follows is typical of such classes.
次の例は、そのようなクラスの典型です。
Complete source code for the intl_example
for this app.
このアプリのintl_exampleの完全なソースコードです。
This example is based on the APIs and tools provided by the intl
package.
この例は、intl
パッケージによって提供されるAPIとツールに基づいています 。
An alternative class for the app’s localized resources describes an example that doesn’t depend on the intl
package.
An alternative class for the app’s localized resourcesでは、intl
パッケージに依存しない例を 説明しています。
The DemoLocalizations
class contains the app’s strings (just one for the example) translated into the locales that the app supports.
このDemoLocalizations
クラスには、アプリがサポートするロケールに翻訳されたアプリの文字列(例では1つだけ)が含まれています。
It uses the initializeMessages()
function generated by Dart’s intl
package, Intl.message()
, to look them up.
工事中🏗
参考