simple_flutter_i18n 1.2.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 63

simple_flutter_i18n #

flutter internationalization made simple and easy to use.

package inspiration #

i got inspired to design the apis of this package by using the amazing package for svelte svelte-i18n

Motive #

while i was searching for the packages to help me with multi-langauge stuff , setting them up was hard, working with them was even harder, so i created this simple package to help me i hope it helps you to.

How to use: #

this class is to be used with the provider package,

PropertiesDescriptionData type
langthe current language,Map<String, dymanic>
isRtlgets the rtl of the current langaugebool

Using it #

the example directory show how to set up the class to work in your project

1- Register your langauges thorugh the regisery class of the package;

import "package:simple_flutter_i18n/registry.dart";

// before your application langauges, register your langauges

  I18nRegistry.register('en', en) // english;
  I18nRegistry.register('ku', ku) // kurdish;
  I18nRegistry.register('ar', ar) // arabic;

by default, the first time the register function is called, it will use that langauge as the initial lanuage of your application. if you explicitly want to set an intiail language, you can call the setInitialLanaugage method;

  I18nRegistry.setInitialLanguage(ar); // arabic is your initial language

then

import "package:simple_flutter_i18n/simple_flutter_i18n.dart";

2- proivde it in the list of providers in your application

MultiProvider(
  providers : [  // other providers of your application
        ChangeNotifierProvider.value(value: I18n())
      ],
  child : MaterialApp(
    // rest of your application
  ),
);

important step #

4 - in the first screen of your application you should call the load method and pass a fallback parameter, which will be used as your app's initial langauge. the best location to do so is the initState of your first screen


MultiProvider(
  providers : [  // other providers of your application
        ChangeNotifierProvider( 
          create: (_) => I18n()
        ) // en is your initial language
      ],
  child : MaterialApp(
    home: HomeScreen()
    // rest of your application
  ),
);

// home_screen.dart

@override
void initState() {
  super.initState();
  
  Provider.of<I18n>(context, listen: false).load(fallback: en); // en is your initial/fallback language in case no langauge was found in the shared_preference registry
}

if there exists a current langauge in the storgae, the package will use that, else it will use the fallback parameter and will call I18n.persist to persist the language.

Optional step #

if you want to listen to langauge changes do this, else you can omit this step

3 - anywhere you need to change the direction of a Widget, wrap it in a Directionality widget


final isRtl = Provider.of<I18n>(context).isRtl;

return Directionality(
  textDirection: isRtl ? TextDirection.rtl : TextDirection.ltr,
  child: YourWidget(),
);

the ideal place to do so is in the root of your project so that your whole application flips on a language change


// in your MaterialApp provider the builder argument

return MaterialApp(
    // rest of your application,
    builder: (context, child) {
      final isRtl = Provider.of<I18n>(context).isRtl;

      return Directionality(
        textDirection: isRtl ? TextDirection.rtl : TextDirection.ltr,
        child: child,
      );
    }
  )
this way all of your application will listen to language change and flip on langauge change

Methods #

I18n #

MethodDescription
setLocalesets the locale for the app and notifies listeners
loadloads the persisted language from storage and sets it
persistpersists the current language to the storage

Registry #

MethodDescription
setInitialLanguagesets the initial language of your application
registerregisters the langauge key-value pair
initialLanguagea getter that will get the initialLanguage of the Regisry class, used by I18n provider class
isEmptychecks if the Regisry is empty or not

1.0.0 #

  • Initial version, created by Stagehand

1.0.0+1 -> ## 1.0.0+6 #

  • refactoring of the package

1.1.0 #

  • added persist method to the package to persist the current language

  • added load method to load the data from storage, using shared_preferences

1.1.0+1 #

  • updated readme for being more informative

1.1.0+2 #

  • fixed a bug in the load method

1.1.0+3 #

  • various bug fixes

1.1.0+4 #

  • removed provider dependancy and updated readme.

1.1.0+5 #

1.1.1+0 #

  • removed the constructor
  • the packages gets initialized through load method

1.1.1+1-hotfix #

  • bug fixes

1.1.1+2-hotfix #

  • bug fixes

1.2.0 #

  • various bug fixes;
  • improved the Readme
  • overall re-structuring of how the package is utilized

1.2.1 #

  • updated readme
  • changed the behaviour of the registry class

1.2.2 #

  • updating flutter and other dependancies

example/main.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../lib/registry.dart';

import '../lib/simple_flutter_i18n.dart';
import './screens/homeScreen.dart';

Map<String, dynamic> en = {'isRtl': false, 'welcome': "Welcome"};
Map<String, dynamic> ku = {'isRtl': true, 'welcome': "Welcome in kurdish"};
Map<String, dynamic> ar = {'isRtl': true, 'welcome': "Welcome in Arabic"};

void main() {
  I18nRegistry.register('en', en);
  I18nRegistry.register('ku', ku);
  I18nRegistry.register('ar', ar);

  I18nRegistry.setInitialLanguage(en);

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  Widget _builder(context, child) {
    final isRtl = Provider.of<I18n>(context).isRtl;

    return Directionality(
      textDirection: isRtl ? TextDirection.rtl : TextDirection.ltr,
      child: child,
    );
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (dd) => I18n())],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(),
        builder: _builder,
        home: HomeScreen(),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  simple_flutter_i18n: ^1.2.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:simple_flutter_i18n/simple_flutter_i18n.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
27
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]
63
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform android

Because of import path [package:simple_flutter_i18n/simple_flutter_i18n.dart, package:simple_flutter_i18n/classes/storage_persister.dart, package:shared_preferences/shared_preferences.dart, package:shared_preferences_linux/shared_preferences_linux.dart] that declares support for platforms: linux

Package does not support Flutter platform ios

Because of import path [package:simple_flutter_i18n/simple_flutter_i18n.dart, package:simple_flutter_i18n/classes/storage_persister.dart, package:shared_preferences/shared_preferences.dart, package:shared_preferences_linux/shared_preferences_linux.dart] that declares support for platforms: linux

Package does not support Flutter platform macos

Because of import path [package:simple_flutter_i18n/simple_flutter_i18n.dart, package:simple_flutter_i18n/classes/storage_persister.dart, package:shared_preferences/shared_preferences.dart, package:shared_preferences_linux/shared_preferences_linux.dart] that declares support for platforms: linux

Package does not support Flutter platform web

Because of import path [package:simple_flutter_i18n/simple_flutter_i18n.dart, package:simple_flutter_i18n/classes/storage_persister.dart, package:shared_preferences/shared_preferences.dart, package:shared_preferences_linux/shared_preferences_linux.dart] that declares support for platforms: linux

Package does not support Flutter platform windows

Because of import path [package:simple_flutter_i18n/simple_flutter_i18n.dart, package:simple_flutter_i18n/classes/storage_persister.dart, package:shared_preferences/shared_preferences.dart] that declares support for platforms: android, ios, linux, macos, web

Package not compatible with SDK dart

because of import path [simple_flutter_i18n] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
shared_preferences ^0.5.7+3 0.5.8
Transitive dependencies
collection 1.14.12 1.14.13
file 5.2.1
flutter_web_plugins 0.0.0
intl 0.16.1
meta 1.1.8 1.2.1
path 1.7.0
path_provider_linux 0.0.1+2
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
shared_preferences_linux 0.0.2+1
shared_preferences_macos 0.0.1+10
shared_preferences_platform_interface 1.0.4
shared_preferences_web 0.1.2+7
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xdg_directories 0.1.0
Dev dependencies
flutter_test