easy_localization icon indicating copy to clipboard operation
easy_localization copied to clipboard

Load Localization Delegate will be called after rebuild page

Open BigGitWorld opened this issue 3 years ago • 5 comments

Hi I wanna change locale by the context.setLocale():

if(context.locale.languageCode == "en"){
    debugPrint("en -----> fa");
    await context.setLocale(const Locale("fa"));
                
}else if(context.locale.languageCode == "fa"){
    debugPrint("fa -----> en");
    await context.setLocale(const Locale("en"));             
}

but I see the below log occurs:

[Easy Localization] [DEBUG]   Load asset from assets/translations
[Easy Localization] [Info]  Locale fa changed
[Easy Localization] [Info]  Locale fa saved
[Easy Localization] [DEBUG]  Build
[Easy Localization] [DEBUG]  Init Localization Delegate
[Easy Localization] [DEBUG]  Init provider
my_flutter_app_log ---> page build () method called             **** here
[Easy Localization] [DEBUG]  Load Localization Delegate         **** and here

Is it true? I think Load Localization Delegate have to be called before rebuilding my page.

Also, I see a strange behavior, so that some texts in my app are translated in reverse. for example: The toolbar text is en when the app locale is changed to the fa and conversely the toolbar text is fa while the app locale is changed to en!

main.dart:

void main() async {
  while (Platform.localeName.isEmpty) {
    await Future.delayed(const Duration(microseconds: 300), () {});
  }
  
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();

  runApp(EasyLocalization(
    child: const ProviderScope(child: MyApp()),
    supportedLocales: const [
      Locale('en'),
      Locale('fa'),
    ],
    path: 'assets/translations', // I have 2 file fa.json & en.json in the assets/translations folder
    fallbackLocale: const Locale('en'),
    saveLocale: true,
    useOnlyLangCode: true,
  ));
}

class MyApp extends ConsumerWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return MaterialApp(
      locale: context.locale,
      supportedLocales: context.supportedLocales,
      localizationsDelegates: context.localizationDelegates,
      initialRoute: "/",
      onGenerateRoute: RouteGenerator.generateRoute,
    );
  }
}

what is the problem?

BigGitWorld avatar May 27 '22 19:05 BigGitWorld

no response?

BigGitWorld avatar Jun 18 '22 17:06 BigGitWorld

I have experienced the same behavior. Some words are translated and others are switched direction without translating the words. But when I do hot reload or hot restart everything works correctly.

tr_issue tr_issue2

Ahmed-Fathy-dev avatar Jun 28 '22 10:06 Ahmed-Fathy-dev

i have the same problem

AliHoseinpoor avatar Oct 11 '22 06:10 AliHoseinpoor

any solutions?

ahmedradwan18 avatar Jun 01 '23 13:06 ahmedradwan18