internationalization 4.0.0 internationalization: ^4.0.0 copied to clipboard
A project to easily implement internationalization on flutter projects
internationalization #
A project to easily implement internationalization on flutter projects
Configure Internationalization #
Go to pubspec.yaml
then do the configurations like this:
internationalization:
output-path: lib/infrastructure/resources/
path: assets/translations
locales:
- locale:
language-code: pt
countries-code:
- BR
- PT
- locale:
language-code: en
- output-path → where de generated file will be placed. Internationalization have a codegen. so run
flutter pub run internationalization
to generate a file that will helps you to translate your strings. If nooutout-path
provided, it'll be placed onlib/
- path → where're the JSON files
- locales → Array of locales. Locales have
language-code
andcountries-code
.- language-code → A string that representes the language (Ex.:
pt
,en
) - countries-code → Array of countries.
countries-code
is optional.
- language-code → A string that representes the language (Ex.:
Now go to main.dart and configure the InternationalizationDelegate
and inform flutter the supported locales. Intl
is the class generated by after ran flutter pub run internationalization
.
MaterialApp(
supportedLocales: _supportedLocales,
localizationsDelegates: [
InternationalizationDelegate(
translationsPath: _translationsPath,
suportedLocales: _supportedLocales,
addTranslations: (locale) async {
//Here you can get some external json and add to internationalization.
//!IMPORTANTE: The json must follow the same json structure on assets.
return {
'external_translate': 'Translation from external source',
};
},
),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
);
IMPORTANT!!
Don't forget to expose the JSON folders
flutter:
uses-material-design: true
assets:
- ./assets/strings/en/
- ./assets/strings/pt/
Folder structure #
The folder structure is very import. So you have to create as same as informed in pubspec.yaml
Translation #
"simple_string".translate(
context,
translationContext: _translationContext,
),
context.translate(
'interpolation_string',
translationContext: _translationContext,
args: ["( ͡° ͜ʖ ͡°)"],
),
context.translate(
'interpolation_string_with_named_args',
translationContext: _translationContext,
namedArgs: {"named_arg_key": "( ͡° ͜ʖ ͡°)"},
),
context.translate(
'simple_plurals',
translationContext: _translationContext,
pluralValue: 0,
),
context.translate(
'simple_plurals',
translationContext: _translationContext,
pluralValue: 1,
),
context.translate(
'simple_plurals',
translationContext: _translationContext,
pluralValue: 123456789,
),
context.translate(
'interpolation_plurals',
translationContext: _translationContext,
pluralValue: 0,
args: ["( ͡° ͜ʖ ͡°)"],
),
context.translate(
'interpolation_plurals',
translationContext: _translationContext,
pluralValue: 1,
args: ["( ͡° ͜ʖ ͡°)"],
),
context.translate(
'interpolation_plurals',
translationContext: _translationContext,
pluralValue: 123456789,
args: ["123456789"],
),
context.translate('no_translate_context'),
```dart
"simple_string".translate()
context.translate()
NumberFormat & DateFormat #
These are features from intl library that was incoporated in Internationalization