simple_flutter_i18n 1.2.3+1 icon indicating copy to clipboard operation
simple_flutter_i18n: ^1.2.3+1 copied to clipboard

a package that makes it easy to have multiple langauges in your application

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
9
likes
110
pub points
6%
popularity

Publisher

unverified uploader

a package that makes it easy to have multiple langauges in your application

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

flutter, shared_preferences

More

Packages that depend on simple_flutter_i18n