androidflutterdartpackageflutter-easy-localization

how to make easy localization button to switch languages in the application?


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!,
      ),
    ),
  );

Solution

  • you need to import easy localization package

    import 'package:easy_localization/easy_localization.dart'
     
    

    Then pass a parameter ('ar' or 'en')

    ElevatedButton(
       onPressed: () {
          setState(() {context.setLocale(Locale('en')); //ar});
                                      },