easy_localization 1.0.2

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

easy_localization #

Easy and Fast internationalizing your Flutter Apps, this package simplify the internationalizing process using Json file

Why easy_localization? #

simplify the internationalizing process in Flutter .

Internationalization by Using JSON Files .

Changelog #

[1.0.2]

  • Added string pluralisation .
  • Added Argument to trans function.

Getting Started #

Configuration #

add


easy_localization: ^1.0.2

you must create a folder in your project's root: the path. Some examples:

/assets/"langs" , "i18n", "locale" or anyname ...

/resources/"langs" , "i18n", "locale" or anyname ...

Inside this folder, must put the json files containing the translated keys :

path/{languageCode}.json

must declare the subtree in your pubspec.yaml as assets:

flutter:
  assets:
    - {`path`/{languageCode}.json}

The next step :

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:easy_localization/easy_localization.dart';

void main() => runApp(EasyLocalization(child: MyApp()));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var data = EasyLocalizationProvider.of(context).data;
    return EasyLocalizationProvider(
      data: data,
      child: MaterialApp(
        title: 'Flutter Demo',
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          //app-specific localization
          EasylocaLizationDelegate(
              locale: data.locale ?? Locale('en'), path: 'resources/langs'),
        ],
        supportedLocales: [Locale('en'), Locale('ar')],
        locale: data.locale,
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(title: 'Easy localization'),
      ),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
 int counter = 0;
  incrementCounter() {
    setState(() {
      counter++;
    });
  }
  @override
  Widget build(BuildContext context) {
    var data = EasyLocalizationProvider.of(context).data;
    return EasyLocalizationProvider(
      data: data,
      child: Scaffold(
        appBar: AppBar(
          title: Text(AppLocalizations.of(context).trans('title')),
          actions: <Widget>[
            FlatButton(
              child: Text("English"),
              color: Localizations.localeOf(context).languageCode == "en"
                  ? Colors.lightBlueAccent
                  : Colors.blue,
              onPressed: () {
                this.setState(() {
                  data.changeLocale(Locale("en"));
                  print(Localizations.localeOf(context).languageCode);
                });
              },
            ),
            FlatButton(
              child: Text("عربى"),
              color: Localizations.localeOf(context).languageCode == "ar"
                  ? Colors.lightBlueAccent
                  : Colors.blue,
              onPressed: () {
                this.setState(() {
                  data.changeLocale(Locale("ar"));
                  print(Localizations.localeOf(context).languageCode);
                });
              },
            )
          ],
        ),
        body: Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Text(AppLocalizations.of(context).trans('msg',arg: 'aissat')),
              new Text(AppLocalizations.of(context).plural('clicked',counter)),
              new FlatButton(
                  onPressed: () async {
                    incrementCounter();
                  },
                  child: new Text(AppLocalizations.of(context).trans('clickMe')),)
            
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(onPressed: incrementCounter,child: Text('+1'),),
      ),
    );
  }
}

Screenshots #

Arbic RTL English LTR

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

example/README.md

example #

example/resources/langs/ar.json #

{
  "title": "السلام",
  "msg":"السلام عليكم يا {}",
  "clickMe":"إضغط هنا",
  "clicked": {
    "zero": "{} نقرة!",
    "one": "{} نقرة!",
    "other": "{} نقرة!"
  }
}

example/resources/langs/ar.json #

{
  "title": "Hello",
  "msg":"Hello {}",
  "clickMe":"Click me",
  "clicked": {
    "zero": "You clicked {} times!",
    "one": "You clicked {} time!",
    "other": "You clicked {} times!"
  }
}

example/lib/main.dart #

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:easy_localization/easy_localization.dart';

void main() => runApp(EasyLocalization(child: MyApp()));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var data = EasyLocalizationProvider.of(context).data;
    return EasyLocalizationProvider(
      data: data,
      child: MaterialApp(
        title: 'Flutter Demo',
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          //app-specific localization
          EasylocaLizationDelegate(
              locale: data.locale ?? Locale('en'), path: 'resources/langs'),
        ],
        supportedLocales: [Locale('en'), Locale('ar')],
        locale: data.locale,
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(title: 'Easy localization'),
      ),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
 int counter = 0;
  incrementCounter() {
    setState(() {
      counter++;
    });
  }
  @override
  Widget build(BuildContext context) {
    var data = EasyLocalizationProvider.of(context).data;
    return EasyLocalizationProvider(
      data: data,
      child: Scaffold(
        appBar: AppBar(
          title: Text(AppLocalizations.of(context).trans('title')),
          actions: <Widget>[
            FlatButton(
              child: Text("English"),
              color: Localizations.localeOf(context).languageCode == "en"
                  ? Colors.lightBlueAccent
                  : Colors.blue,
              onPressed: () {
                this.setState(() {
                  data.changeLocale(Locale("en"));
                  print(Localizations.localeOf(context).languageCode);
                });
              },
            ),
            FlatButton(
              child: Text("عربى"),
              color: Localizations.localeOf(context).languageCode == "ar"
                  ? Colors.lightBlueAccent
                  : Colors.blue,
              onPressed: () {
                this.setState(() {
                  data.changeLocale(Locale("ar"));
                  print(Localizations.localeOf(context).languageCode);
                });
              },
            )
          ],
        ),
        body: Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Text(AppLocalizations.of(context).trans('msg',arg: 'aissat')),
              new Text(AppLocalizations.of(context).plural('clicked',counter)),
              new FlatButton(
                  onPressed: () async {
                    incrementCounter();
                  },
                  child: new Text(AppLocalizations.of(context).trans('clickMe')),)
            
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(onPressed: incrementCounter,child: Text('+1'),),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  easy_localization: ^1.0.2

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:easy_localization/easy_localization.dart';
  
Version Uploaded Documentation Archive
1.0.3+1 Feb 7, 2019 Go to the documentation of easy_localization 1.0.3+1 Download easy_localization 1.0.3+1 archive
1.0.3 Jan 17, 2019 Go to the documentation of easy_localization 1.0.3 Download easy_localization 1.0.3 archive
1.0.2+4 Jan 17, 2019 Go to the documentation of easy_localization 1.0.2+4 Download easy_localization 1.0.2+4 archive
1.0.2+3 Jan 2, 2019 Go to the documentation of easy_localization 1.0.2+3 Download easy_localization 1.0.2+3 archive
1.0.2+2 Dec 26, 2018 Go to the documentation of easy_localization 1.0.2+2 Download easy_localization 1.0.2+2 archive
1.0.2+1 Dec 25, 2018 Go to the documentation of easy_localization 1.0.2+1 Download easy_localization 1.0.2+1 archive
1.0.2 Dec 25, 2018 Go to the documentation of easy_localization 1.0.2 Download easy_localization 1.0.2 archive
1.0.1 Dec 22, 2018 Go to the documentation of easy_localization 1.0.1 Download easy_localization 1.0.1 archive
1.0.0 Dec 22, 2018 Go to the documentation of easy_localization 1.0.0 Download easy_localization 1.0.0 archive
0.0.1 Dec 22, 2018 Go to the documentation of easy_localization 0.0.1 Download easy_localization 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
88
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

We analyzed this package on May 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.1
  • pana: 0.12.16
  • Flutter: 1.5.4-hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/easy_localization_delegate.dart. (-1 points)

Analysis of lib/easy_localization_delegate.dart reported 2 hints:

line 17 col 3: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

line 58 col 3: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

Format lib/easy_localization.dart.

Run flutter format to format lib/easy_localization.dart.

Format lib/easy_localization_provider.dart.

Run flutter format to format lib/easy_localization_provider.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
flutter_localizations 0.0.0
Transitive dependencies
collection 1.14.11
intl 0.15.8
meta 1.1.6 1.1.7
path 1.6.2
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test