flutter_i18n 0.2.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new80

flutter_i18n

I18n made easy, for Flutter!


Why you should use flutter_i18n?

The main goal of flutter_i18n is to simplify the i18n process in Flutter. I would like to recreate the same experience that you have with the Angular i18n: simple json files, one for each language that you want to support.

Configuration

To use this library, you must create in your project's root the following subfolders:

/assets/flutter_i18n

and put inside them the json related to the translation, using the following configuration:

  • If you want to specify the country code

    /assets/flutter_i18n/{languageCode}_{countryCode}.json

  • otherwise

    /assets/flutter_i18n/{languageCode}.json

Of course, you must declare the subtree in your pubspec.yaml as assets:

flutter:
  assets:
    - assets/flutter_i18n/

The next step consist in the configuration of the localizationsDelegates; to use flutter_i18n, you should configure as follows:

localizationsDelegates: [
        FlutterI18nDelegate(useCountryCode, [fallbackFile]),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
],

The useCountryCode parameter depends on the json configuration:

  • if you used the pattern {languageCode}_{countryCode}, useCountryCode must be true
  • if you used the pattern {languageCode}, useCountryCode must be false

The fallbackFile parameter was entroduces with the version 0.1.0 and provide a default language, used when the translation for the current running system is not provided. This should contain the name of a valid json file in assets folder.

If there isn't any translation available for the required key, the same key is returned.

flutter_i18n in action

After the configuration steps, the only thing to do is invoke the following method:

FlutterI18n.translate(buildContext, "your.key")

Where:

  • buildContext is the BuildContext instance of the widget
  • your.key is the key to translate

From version 0.2.0 flutter_i18n manage strings that contain parameters; an example can be: "Hello, ***{user}***!" For a correct translation, you must use the third parameter of the translate method, a Map<String, String> where:

  • the keys are the placeholders used in the .json file (i.e. user)
  • the values are what you want to display

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_i18n/flutter_i18n_delegate.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
      localizationsDelegates: [
        FlutterI18nDelegate(false, 'en'),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar:
          new AppBar(title: new Text(FlutterI18n.translate(context, "title"))),
      body: new Builder(builder: (BuildContext context) {
        return new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Text(FlutterI18n.translate(context, "label.main",  Map.fromIterables(["user"], ["Flutter lover"]))),
              new FlatButton(
                  onPressed: () {
                    Scaffold.of(context).showSnackBar(new SnackBar(
                          content: new Text(
                              FlutterI18n.translate(context, "toastMessage")),
                        ));
                  },
                  child: new Text(
                      FlutterI18n.translate(context, "button.clickMe")))
            ],
          ),
        );
      }),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_i18n: ^0.2.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_i18n/flutter_i18n.dart';
  
Version Uploaded Documentation Archive
0.2.0 Sep 23, 2018 Go to the documentation of flutter_i18n 0.2.0 Download flutter_i18n 0.2.0 archive
0.1.0 Sep 21, 2018 Go to the documentation of flutter_i18n 0.1.0 Download flutter_i18n 0.1.0 archive
0.0.3 Sep 10, 2018 Go to the documentation of flutter_i18n 0.0.3 Download flutter_i18n 0.0.3 archive
0.0.2 Aug 27, 2018 Go to the documentation of flutter_i18n 0.0.2 Download flutter_i18n 0.0.2 archive
0.0.1 Aug 26, 2018 Go to the documentation of flutter_i18n 0.0.1 Download flutter_i18n 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
60
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]
80
Learn more about scoring.

We analyzed this package on Sep 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.3
  • Flutter: 0.8.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <3.0.0
flutter 0.0.0
flutter_localizations 0.0.0
Transitive dependencies
collection 1.14.11
intl 0.15.7
meta 1.1.6
path 1.6.2
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test