text
stringlengths
1
474
GlobalMaterialLocalizations.delegate,
NnMaterialLocalizations.delegate, // Add the newly created delegate
],
supportedLocales: [
Locale('en', 'US'),
Locale('nn'),
],
home: Home(),
),<code_end>
<topic_end>
<topic_start>
Alternative internationalization workflows
This section describes different approaches to internationalize
your Flutter application.<topic_end>
<topic_start>
An alternative class for the app’s localized resources
The previous example was defined in terms of the Dart intl
package. You can choose your own approach for managing
localized values for the sake of simplicity or perhaps to integrate
with a different i18n framework.Complete source code for the minimal app.In the following example, the DemoLocalizations class
includes all of its translations directly in per language Maps:
<code_start>class DemoLocalizations {
DemoLocalizations(this.locale);
final Locale locale;
static DemoLocalizations of(BuildContext context) {
return Localizations.of<DemoLocalizations>(context, DemoLocalizations)!;
}
static const _localizedValues = <String, Map<String, String>>{
'en': {
'title': 'Hello World',
},
'es': {
'title': 'Hola Mundo',
},
};
static List<String> languages() => _localizedValues.keys.toList();
String get title {
return _localizedValues[locale.languageCode]!['title']!;
}
}<code_end>
In the minimal app the DemoLocalizationsDelegate is slightly
different. Its load method returns a SynchronousFuture
because no asynchronous loading needs to take place.
<code_start>class DemoLocalizationsDelegate
extends LocalizationsDelegate<DemoLocalizations> {
const DemoLocalizationsDelegate();
@override
bool isSupported(Locale locale) =>
DemoLocalizations.languages().contains(locale.languageCode);
@override
Future<DemoLocalizations> load(Locale locale) {
// Returning a SynchronousFuture here because an async "load" operation
// isn't needed to produce an instance of DemoLocalizations.
return SynchronousFuture<DemoLocalizations>(DemoLocalizations(locale));
}
@override
bool shouldReload(DemoLocalizationsDelegate old) => false;
}<code_end>
<topic_end>
<topic_start>
Using the Dart intl tools
Before building an API using the Dart intl package,
review the intl package’s documentation.
The following list summarizes the process for
localizing an app that depends on the intl package:The demo app depends on a generated source file called
l10n/messages_all.dart, which defines all of the
localizable strings used by the app.Rebuilding l10n/messages_all.dart requires two steps.With the app’s root directory as the current directory,
generate l10n/intl_messages.arb from lib/main.dart:The intl_messages.arb file is a JSON format map with one entry for
each Intl.message() function defined in main.dart.
This file serves as a template for the English and Spanish translations,
intl_en.arb and intl_es.arb.
These translations are created by you, the developer.With the app’s root directory as the current directory,
generate intl_messages_<locale>.dart for each
intl_<locale>.arb file and intl_messages_all.dart,
which imports all of the messages files:Windows doesn’t support file name wildcarding.
Instead, list the .arb files that were generated by the
intl_translation:extract_to_arb command.The DemoLocalizations class uses the generated
initializeMessages() function
(defined in intl_messages_all.dart)
to load the localized messages and Intl.message()
to look them up.<topic_end>
<topic_start>
More information
If you learn best by reading code,
check out the following examples.If Dart’s intl package is new to you,
check out Using the Dart intl tools.
<topic_end>
<topic_start>State management
<topic_end>
<topic_start>
Topics
<topic_end>
<topic_start>State management
info Note
If you have written a mobile app using Flutter
and wonder why your app’s state is lost
on a restart, check out Restore state on Android
or Restore state on iOS.If you are already familiar with state management in reactive apps,
you can skip this section, though you might want to review the
list of different approaches.As you explore Flutter,