text
stringlengths
1
372
for more information about localization strings,
check out the flutter_localizations README.
once you’ve implemented your language-specific subclasses of
GlobalMaterialLocalizations and LocalizationsDelegate,
you need to add the language and a delegate instance to your app.
the following code sets the app’s language to nynorsk and
adds the NnMaterialLocalizations delegate instance to the app’s
localizationsDelegates list:
<code_start>
const MaterialApp(
localizationsDelegates: [
GlobalWidgetsLocalizations.delegate,
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: