A minimal, lightweight, easy to use, dart extension for internationalization
flutter internationalization made simple and easy to use.
i got inspired to design the apis of this package by using the amazing package for svelte svelte-i18n
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.
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 |
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
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
),
);
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.
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,
);
}
)
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 |
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 |