Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language not updated for some widgets #88

Closed
erperejildo opened this issue May 29, 2022 · 6 comments
Closed

Language not updated for some widgets #88

erperejildo opened this issue May 29, 2022 · 6 comments

Comments

@erperejildo
Copy link

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

@stale
Copy link

stale bot commented Jul 28, 2022

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.

@stale stale bot added the wontfix This will not be worked on label Jul 28, 2022
@stale stale bot closed this as completed Aug 4, 2022
@erperejildo
Copy link
Author

still having the same problem

@bratan
Copy link
Member

bratan commented Nov 8, 2023

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

@bratan bratan reopened this Nov 8, 2023
@stale stale bot removed wontfix This will not be worked on labels Nov 8, 2023
@erperejildo
Copy link
Author

@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

@bratan
Copy link
Member

bratan commented Nov 8, 2023

@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'),

@erperejildo
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants