Giter Club home page Giter Club logo

flutter_i18n's Introduction

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 a folder in your project's root: the basePath. Some examples:

/assets/flutter_i18n (the default one)

/assets/i18n

/assets/locales

Inside this folder, you'll put the json or yaml files containing the translated keys. You have two options:

  • If you want to specify the country code

    basePath/{languageCode}_{countryCode}.json

  • otherwise

    basePath/{languageCode}.json

If the json file is not available, we will look for a yaml file with the same name. In case both exist, the json file will be used.

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

flutter:
  assets:
    - {basePath}

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

localizationsDelegates: [
        FlutterI18nDelegate({useCountryCode = false, fallbackFile, basePath = "assets/flutter_i18n"}),
        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.

The basePath parameter is optionally used to set the base path for translations. If this option is not set, the default path will be assets/flutter_i18n. This path must be the same path as the one defined in your pubspec.yaml.

The forcedLocale parameter is optionally used to force a locale instead finding the system one.

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

Other examples of use:

await FlutterI18n.refresh(buildContext, languageCode, {countryCode});
FlutterI18n.plural(buildContext, "your.key", pluralValue);

For more informations, read the CHANGELOG.md.

flutter_i18n's People

Contributors

ilteoood avatar j4nk3e avatar jlcool avatar lucasvienna avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.