internationalization 3.0.0+1
internationalization: ^3.0.0+1 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 internationalizationto generate a file that will helps you to translate your strings. If nooutout-pathprovided, it'll be placed onlib/ - path → where're the JSON files
- locales → Array of locales. Locales have
language-codeandcountries-code.- language-code → A string that representes the language (Ex.:
pt,en) - countries-code → Array of countries.
countries-codeis 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: Intl.suportedLocales,
localizationsDelegates: [
InternationalizationDelegate(
translationsPath: Intl.stringsPath,
suportedLocales: Intl.suportedLocales,
files: Intl.files,
),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
);
IMPORTANT!!
Don't forget to expose the JSON files
flutter:
uses-material-design: true
assets:
- ./assets/strings/en/
- ./assets/strings/pt/BR/
Folder structure #
The folder structure is very import. So you have to create as same as informed in pubspec.yaml
[]
Translation #
Intl.usage.simpleString()
Intl.usage.interpolationString(args: ["( ͡° ͜ʖ ͡°)"])
Intl.usage.interpolationStringWithNamedArgs(
namedArgs: {"named_arg_key": "( ͡° ͜ʖ ͡°)"},
),
Intl.usage.simplePlurals(pluralValue: 0)),
Intl.usage.simplePlurals(pluralValue: 1)),
Intl.usage.simplePlurals(pluralValue: 123456789)),
Intl.usage.interpolationPlurals(
pluralValue: 0,
args: ["( ͡° ͜ʖ ͡°)"],
),
Intl.usage.interpolationPlurals(
pluralValue: 1,
args: ["( ͡° ͜ʖ ͡°)"],
),
Intl.usage.interpolationPlurals(
pluralValue: 123456789,
args: ["123456789"],
),
You also use extensions on strings to translate. Just use a string with the key and call .translate()
"simple_string".translate()
BuildContext #
You moust have to, or not, inform the BuildContext to Internationalization. If you do, it allows Internationalization to listener when you change language of your app and avoid to close and reopen the app to apply the changes.
Internationalization.of(context);
To inform BuildContext just do it in every new screen (don't need to do in a simple widget)
NumberFormat & DateFormat #
These are features from intl library that was incoporated in Internationalization