flutter_i18next

Last updated:

0 purchases

flutter_i18next Image
flutter_i18next Images
Add to Cart

Description:

flutter i18next

flutter_i18next #
A package to bring i18next support for Flutter! This is heavily based on flutter_i18n but with lots of modification and simplifications.

Usage #
First, Add flutter_i18next to your pubspec.yaml:
dependencies:
flutter_i18next: ^0.3.0
copied to clipboard
Next, Add an instance of I18NextDelegate to your app's localizationsDelegates:
MaterialApp(
supportedLocales: [
Locale('en'),
Locale('fa'),
],
localizationsDelegates: [
I18NextDelegate(
translationLoader: FileTranslationLoader(
useCountryCode: false,
basePath: 'assets/i18n',
),
),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
)
copied to clipboard
Now you can get the translation using the extension method on BuildContext or the I18Next class directly:
I18Next.t(context, 'label.main')
//or
context.t('label.main')
copied to clipboard
Accessing Key #
You can pass a normal key or a deep key with a default value and a list of fallback keys when retrieving translation:
I18Next.t(context, 'deep.key', defaultValue: 'value', fallbackKeys: ['key1', 'key2'])
copied to clipboard
Namespaces are not supported yet.
Interpolation #
Only the basic interpolation from i18next is supported:
I18Next.t(context, 'key', params: {'param': 'value'})
copied to clipboard
Formatting #
You can provide an instance of InterpolationOptions to handle formatting.
I18NextDelegate(
interpolationOptions:
InterpolationOptions(formatter: (value, format, locale) {
if (format == 'uppercase') {
return value.toString().toUpperCase();
} else if (value is DateTime) {
return DateFormat(format).format(value);
}
return value;
}),
)
copied to clipboard
Plurals #
Right now, it's only possible to define singular and plural key:
I18Next.t('key', count: 2)
copied to clipboard
Interval plurals and Languages with multiple plurals are not supported yet.
Locale Changing #
It's also possible to use the I18NextLocaleBuilder widget provided with the plugin to handle locale changes. First you need to wrap your app inside a I18NextLocaleBuilder:
I18NextLocaleBuilder(
defaultLocale: Locale('en'),
builder: (context, locale) => MaterialApp(
locale: locale,
...
),
)
copied to clipboard
Now to change the locale you can use:
I18NextLocaleBuilder.of(context).locale = Locale('en');
//or
context.locale = Locale('en');
copied to clipboard
The given locale should be in the supportedLocales of your app and your app (MaterialApp or CupertinoApp) will take care of the rest (loading translations using delegator and changing app direction).

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.