项目作者: A7madXatab

项目描述 :
A minimal, lightweight, easy to use, dart extension for internationalization
高级语言: Dart
项目地址: git://github.com/A7madXatab/simple_flutter_i18n.git
创建时间: 2020-02-08T16:13:40Z
项目社区:https://github.com/A7madXatab/simple_flutter_i18n

开源协议:MIT License

下载


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,

Properties Description Data type
lang the current language, Map
isRtl gets the rtl of the current langauge bool

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;

  1. import "package:simple_flutter_i18n/registry.dart";
  2. // before your application langauges, register your langauges
  3. I18nRegistry.register('en', en) // english;
  4. I18nRegistry.register('ku', ku) // kurdish;
  5. 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;

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

then

  1. import "package:simple_flutter_i18n/simple_flutter_i18n.dart";

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

  1. MultiProvider(
  2. providers : [ // other providers of your application
  3. ChangeNotifierProvider.value(value: I18n())
  4. ],
  5. child : MaterialApp(
  6. // rest of your application
  7. ),
  8. );

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

  1. MultiProvider(
  2. providers : [ // other providers of your application
  3. ChangeNotifierProvider(
  4. create: (_) => I18n()
  5. ) // en is your initial language
  6. ],
  7. child : MaterialApp(
  8. home: HomeScreen()
  9. // rest of your application
  10. ),
  11. );
  12. // home_screen.dart
  13. @override
  14. void initState() {
  15. super.initState();
  16. 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
  17. }

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

  1. final isRtl = Provider.of<I18n>(context).isRtl;
  2. return Directionality(
  3. textDirection: isRtl ? TextDirection.rtl : TextDirection.ltr,
  4. child: YourWidget(),
  5. );

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

  1. // in your MaterialApp provider the builder argument
  2. return MaterialApp(
  3. // rest of your application,
  4. builder: (context, child) {
  5. final isRtl = Provider.of<I18n>(context).isRtl;
  6. return Directionality(
  7. textDirection: isRtl ? TextDirection.rtl : TextDirection.ltr,
  8. child: child,
  9. );
  10. }
  11. )
this way all of your application will listen to language change and flip on langauge change

Methods

I18n

Method Description
setLocale sets the locale for the app and notifies listeners
load loads the persisted language from storage and sets it
persist persists the current language to the storage

Registry

Method Description
setInitialLanguage sets the initial language of your application
register registers the langauge key-value pair
initialLanguage a getter that will get the initialLanguage of the Regisry class, used by I18n provider class
isEmpty checks if the Regisry is empty or not