Jesway / flutter_translate

Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter.
MIT License
403 stars 118 forks source link

Language not updated for some widgets #88

Closed erperejildo closed 1 year ago

erperejildo commented 2 years ago

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[bot] commented 2 years ago

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.

erperejildo commented 1 year ago

still having the same problem

bratan commented 1 year ago

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

erperejildo commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

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