i18n_localizations 1.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 78

i18n_localizations #

A flutter package for internationalization, it allows you to simply add the translation of the application according to the language of the device, it uses json files as angular. You only need to configure it properly to be able to translate your entire app.

Getting Started #

Follow the next step for use i18n_localizations

Configuration #

To use this library, you will need json file you can put there where you want, but don't forget to add them to your pubspec.yaml

assets:
  - {translateDir}/en.json
  - {translateDir}/fr.json
  - {translateDir}/{languageCode}.json
    or
  - {translateDir}/en_US.json
  - {translateDir}/fr_FR.json
  - {translateDir}/{languageCode}_${countryCode}.json

You need to initialize a I18nLocalizationsDelegate white a list of supportedLocales and a pathFile where the json file are store.

final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en'), Locale('fr')],
      pathFile: '{translateDir}'
  );
or
final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en', 'US'), Locale('fr', 'FR')],
      pathFile: '{translateDir}',
      haveCountryCode: true
  );

Then you need to configure the supportedLocales, localizationsDelegates and localeResolutionCallback of your MaterialApp or CupertinoApp

  supportedLocales: i18n.supportedLocales,
  localizationsDelegates: [
   i18n,
   GlobalMaterialLocalizations.delegate,
   GlobalWidgetsLocalizations.delegate
  ],
  localeResolutionCallback: (Locale locale, supportedLocales) {
    return i18n.localeResolutionCallback(locale);
  },

Result

// construct the i18nLocalisationsDelegate
final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en', 'US'), Locale('fr', 'FR')],
      pathFile: '{translateDir}'
  );
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'MyApp',
        onGenerateRoute: router.generateRoute,
        debugShowCheckedModeBanner: false,
        initialRoute: HomePageRoute,
        // set the supportedLocales for the app
        supportedLocales: i18n.supportedLocales,
        localizationsDelegates: [
         // don't forget to put the delegate
          i18n,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
        localeResolutionCallback: (Locale locale, supportedLocales) {
           // get the default local when the device language is not supported, use the first one
          // from the list (English, in this case).
          return i18n.localeResolutionCallback(locale);
        },
    );
  }

Use #

To use I18nLocalizations, just call :

I18nLocalizations.translate(context,"keySimple")
I18nLocalizations.translate(context,"key.child_one")
I18nLocalizations.translate(context,"key.child_two.grandchild")

I18nLocalizations.translate(context,"keyParams", params:{"key": "value"})

I18nLocalizations.translatePlural(context,"keyPlural", params:{"key":"6"})
I18nLocalizations.translatePlural(context,"keyPlural", params:{"key":"1"})
I18nLocalizations.translatePlural(context,"keyPlural", params:{"key":"not a number"})

And the json file will look like:

{
  "keySimple": "key simple",
  "key": {
    "child_one": "child 1",
    "child_two": {
      "grandchild": "grandchild of key"
    }
  },
  "keyParams": "keyParams have a params or a {key}",
  "keyPlural": {
    "none": "there are none key here",
    "one": "the value of our key is {key}",
    "many": "we have {key} keys"
  }
}

Note: there should be no space between the key and the parameter hooks

[1.1.1] - 24 May 2020 #

Add Options for CountryCode

assets:
  - {translateDir}/en.json
  - {translateDir}/fr.json
  - {translateDir}/{languageCode}.json
  or
  - {translateDir}/en_US.json
  - {translateDir}/fr_FR.json
  - {translateDir}/{languageCode}_${countryCode}.json
final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en'), Locale('fr')],
      pathFile: '{translateDir}'
  );
  or
  final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
        supportedLocales: [Locale('en', 'US'), Locale('fr', 'FR')],
        pathFile: '{translateDir}',
        haveCountryCode: true
    );

[1.1.0] - 6 Mars 2020 #

Add Plural by function translatePlural

I18nLocalizations.translatePlural(context,"keyPlural", params:{"key":"6"})
I18nLocalizations.translatePlural(context,"keyPlural", params:{"key":"1"})
I18nLocalizations.translatePlural(context,"keyPlural", params:{"key":"not a number"})

And the json file will look like:

{
  "keyPlural": {
    "none": "there are none key here",
    "one": "the value of our key is {key}",
    "many": "we have {key} keys"
  }
}

Note: there should be no space between the key and the parameter hooks

[1.0.2] - 6 MARS 2020 #

Add Example

[1.0.1] - 5 MARS 2020 #

Format file lib

[1.0.0] - 5 MARS 2020 #

i18n_localizations allows you to simply add the translation of the application according to the language of the device, it uses json files as angular. You only need to configure it properly to be able to translate your entire app.

Configuration #

To use this library, you wiil need json file you can put there where you want, but don't forget to add them to your pubspec.yaml

assets:
  - {translateDir}/en.json
  - {translateDir}/fr.json
  - {translateDir}/{languageCode}.json

You need to initialize a I18nLocalizationsDelegate white a list of supportedLocales and a pathFile where the json file are store.

final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en', 'US'), Locale('fr', 'FR')],
      pathFile: '{translateDir}'
  );

Then you need to configure the supportedLocales, localizationsDelegates and localeResolutionCallback of your MaterialApp or CupertinoApp

  supportedLocales: i18n.supportedLocales,
  localizationsDelegates: [
   i18n,
   GlobalMaterialLocalizations.delegate,
   GlobalWidgetsLocalizations.delegate
  ],
  localeResolutionCallback: (Locale locale, supportedLocales) {
    return i18n.localeResolutionCallback(locale);
  },

Result

// construct the i18nLocalisationsDelegate
final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en', 'US'), Locale('fr', 'FR')],
      pathFile: 'assets/i18n'
  );
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'MyApp',
        onGenerateRoute: router.generateRoute,
        debugShowCheckedModeBanner: false,
        initialRoute: HomePageRoute,
        // set the supportedLocales for the app
        supportedLocales: i18n.supportedLocales,
        localizationsDelegates: [
         // don't forget to put the delegate
          i18n,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
        localeResolutionCallback: (Locale locale, supportedLocales) {
           // get the default local when the device language is not supported, use the first one
          // from the list (English, in this case).
          return i18n.localeResolutionCallback(locale);
        },
    );
  }

Use #

To use I18nLocalizations, just call :

I18nLocalizations.translate(context,"keySimple")
I18nLocalizations.translate(context,"key.child_one")
I18nLocalizations.translate(context,"key.child_two.grandchild")

I18nLocalizations.translate(context,"keyParams", params:{"key": "value"})

And the json file will look like:

{
  "keySimple": "key simple",
  "key": {
    child_one: "child 1",
    child_two: {
      grandchild: "grandchild of key"
    }
  },
  "keyParams": "keyParams have a params or a {key}"
}

Note: there should be no space between the key and the parameter hooks

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:i18n_localizations/i18n_localizations.dart';
import 'package:i18n_localizations/i18n_localizations_delegate.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en'), Locale('fr')], pathFile: 'assets/i18n');
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
      supportedLocales: i18n.supportedLocales,
      localizationsDelegates: [
        i18n,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      localeResolutionCallback: (Locale locale, supportedLocales) {
        return i18n.localeResolutionCallback(locale);
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              I18nLocalizations.translate(context, "keySimple"),
            ),
            Text(I18nLocalizations.translate(context, "key.child_one")),
            Text(I18nLocalizations.translate(
                context, "key.child_two.grandchild")),
            Text(I18nLocalizations.translate(context, "keyParams",
                params: {"testkey": "value"})),
            Text(I18nLocalizations.translatePlural(context, "keyPlural",
                params: {"key": "not a number"})),
            Text(I18nLocalizations.translatePlural(context, "keyPlural",
                params: {"key": "1"})),
            Text(I18nLocalizations.translatePlural(context, "keyPlural",
                params: {"key": "6"})),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  i18n_localizations: ^1.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:i18n_localizations/i18n_localizations.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
55
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
78
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • i18n_localizations that is a package requiring null.

Health suggestions

Format lib/generated/i18n.dart.

Run flutter format to format lib/generated/i18n.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_localizations 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
intl 0.16.1
meta 1.1.8 1.2.2
path 1.6.4 1.7.0
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test