Jesway / flutter_translate

Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Language not updated for some widgets

erperejildo opened this issue · comments

Main page and back icon description (this is automatic so no changes from my side) are still not updated when the rest of the app is. I need to restart the app to see the new language.

Video of the issue:
https://www.youtube.com/shorts/fFOS5KXPHo8

Language change function:

onChanged: (newLang) async {
                    final loading = new Loading();
                    await loading.load(
                      context,
                      translate('options_page.updating_language'),
                    );
                    // update dropdown language
                    _language = newLang;
                    // update app language
                    await changeLocale(context,
                        newLang);
                    Locale(newLang!, '');
                    // save language
                    _options["language"] = newLang;
                    await prefs.setString('options', json.encode(_options));
                    Provider.of<Game>(context, listen: false)
                        .initGame(context, newLang, true);
                    loading.cancel(context);
                  },

More info:

  final delegate = await LocalizationDelegate.create(
       fallbackLocale: 'en',
       supportedLocales: languages,
     );
   
     runZonedGuarded(() {
       runApp(
         LocalizedApp(
           delegate,
           MyApp(),
         ),
       );
     }, FirebaseCrashlytics.instance.recordError);
   ...
   MaterialApp(
           locale: getSavedLanguage(),
           supportedLocales: localizationDelegate.supportedLocales,
           localizationsDelegates: [
             GlobalMaterialLocalizations.delegate,
             GlobalWidgetsLocalizations.delegate,
             GlobalCupertinoLocalizations.delegate,
             localizationDelegate
           ],
   ...
   getSavedLanguage() {
     var options = prefs.get('options');
     // first time we initiate the app there are no default options yet so we create them
     if (options == null) {
       options = json.encode({
         'language': ui.window.locale.languageCode, // default phone language
         'sounds': true,
         'fastMode': false
       });
       prefs.setString('options', options.toString());
     }
     final Map optionsMap = json.decode(options.toString());
     return Locale(optionsMap['language'], '');
   }

and:

flutter_translate: ^4.0.2

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

still having the same problem

@erperejildo - can you reproduce this issue in a sample project? I will take a look.

@erperejildo - can you reproduce this issue in a sample project? I will take a look.

https://zapp.run/edit/flutter-z7ec06l27ed0?entry=lib/main.dart&file=lib/options.dart

@erperejildo - can you reproduce this issue in a sample project? I will take a look.

https://zapp.run/edit/flutter-z7ec06l27ed0?entry=lib/main.dart&file=lib/options.dart

Here is a working example and you can find the fixes below:

https://zapp.run/edit/flutter-zwek06ldwel0?entry=lib/main.dart&file=lib/main.dart

  1. You need to wrap the MaterialApp into a LocalizationProvider:
 return LocalizationProvider(
      state: LocalizationProvider.of(context).state,
      child:  MaterialApp(
        title: 'Flutter App!!',
        locale: localizationDelegate.currentLocale,
  1. MyHomePage should not be a constant

home: MyHomePage(title: 'Flutter Example App'),

instead of

home: const MyHomePage(title: 'Flutter Example App'),

https://zapp.run/edit/flutter-z7ec06l27ed0?entry=lib/main.dart&file=assets/i18n/en.json:59-105

@erperejildo - can you reproduce this issue in a sample project? I will take a look.

https://zapp.run/edit/flutter-z7ec06l27ed0?entry=lib/main.dart&file=lib/options.dart

Here is a working example and you can find the fixes below:

https://zapp.run/edit/flutter-zwek06ldwel0?entry=lib/main.dart&file=lib/main.dart

  1. You need to wrap the MaterialApp into a LocalizationProvider:
 return LocalizationProvider(
      state: LocalizationProvider.of(context).state,
      child:  MaterialApp(
        title: 'Flutter App!!',
        locale: localizationDelegate.currentLocale,
  1. MyHomePage should not be a constant

home: MyHomePage(title: 'Flutter Example App'),

instead of

home: const MyHomePage(title: 'Flutter Example App'),

Forgot to wrap it in the example but what I had in my code was the const since it was suggesting me to add it. Thanks, working now