flutter_i18next 0.3.0 copy "flutter_i18next: ^0.3.0" to clipboard
flutter_i18next: ^0.3.0 copied to clipboard

An internationalization package for Flutter using i18next standards.

flutter_i18next #

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

pub package

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).

3
likes
140
points
46
downloads

Publisher

unverified uploader

Weekly Downloads

2024.09.16 - 2025.03.31

An internationalization package for Flutter using i18next standards.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, universal_io, xml2json, yaml

More

Packages that depend on flutter_i18next