I’m using easy Localization Package in 2 languages Application ,,And I Need To swith langauge using a Button . How could i Do That?
await EasyLocalization.ensureInitialized();
log(token);
runApp(
EasyLocalization(
supportedLocales: const [Locale('ar'), Locale('en')],
path: 'assets/translations',
startLocale: const Locale('ar'),
fallbackLocale: const Locale('en'),
saveLocale: true,
assetLoader: const CodegenLoader(),
child: ScreenUtilInit(
designSize: const Size(411.4, 683.4),
child: const MyApp(),
builder: (context, child) => child!,
),
),
);
2
Answers
There is lesson explain the right way to make it:
Source_code_in_github
Explain Localization with provider and shared preferences
There are some steps you should follow:
provider
andshared_preferneces
.l10n
.l10n
folder as*.arb
i.eapp_ar.arb
andapp_en.arb
.l10n
folder name it:l10n.dart
.arb
files like this:"youKey":"your_value
first letter of key must be small letter camelCase, no _ nor -. i.el10n.dart
.l10n.yaml
file in the root space of your project and write in it:Then in your terminal run
flutter pub get
that will generate the classes that contain all you properties of your languages.Add new dart file name i.e
app_local.dart
with this code:setting_provider.dart
:shared_pref.dart
:main
function:and then in
MyApp
class return the provider like:HomePage
:you need to import easy localization package
Then pass a parameter (‘ar’ or ‘en’)