Jesway / flutter_translate

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

Add an option to automatically sync the current locale with the system locale #21

Open bratan opened 4 years ago

bratan commented 4 years ago

Add an option to automatically sync the current locale with the system locale.

So when the system locale is changed, the current/app locale will be reloaded as well (if supported).

Reference: https://github.com/bratan/flutter_translate/issues/20

diegoveloper commented 4 years ago

Can you detect the language change in localeResolutionCallback then rebuild the LocalizationProvider ?

bratan commented 4 years ago

There are some issues preventing this from working currently but yes, I think this will be the way to go.

doc-rj-celltrak commented 4 years ago

Thanks for the great package, we may be using it in our app. Is this enhancement still planned?

kuhnroyal commented 4 years ago

@bratan Can you elaborate on why the flutter_device_locale package is needed. The same functionality and the syncing is available in plain Flutter via WidgetsBinding.instance.addObserver(...) or am I missing something?

bratan commented 4 years ago

@kuhnroyal - Because of some iOS issues where different values for the current/selected locale are returned depending on the environment (debug/release) and device type simulator/real device.

So by using flutter_device_locale the real locale is always used regardless of the environment.

More info: https://github.com/flutter/flutter/issues/14128 https://github.com/bratan/flutter_translate/pull/15

kuhnroyal commented 4 years ago

@bratan Thanks, Right I now remember this issue. I though this is solved by adding the correct plist entries tho.

diegoveloper commented 4 years ago

@bratan what about doing this

    final currentLocale = WidgetsBinding.instance.window.locale;
    final locale = currentLocale != null && currentLocale.countryCode != null
        ? currentLocale
        : Localizations.localeOf(context);

to avoid using flutter_device_locale , btw any progress on this issue? thanks for this great package!

doc-rj-celltrak commented 4 years ago

Hi @bratan is there any update on this issue? Thanks.

bratan commented 4 years ago

Hi, there is no update currently as I`m quite busy these days, probably in the near future.

doc-rj-celltrak commented 3 years ago

If anyone wants to implement this themselves, you can use a WidgetsBindingObserver and override didChangeLocales(). Do this with a StatefulWidget below LocalizedApp and above MaterialApp.

When a system locale change is detected, you can do something like this:

    Future<void> _didChangeLocales(
    BuildContext context,
    List<Locale>? locales,
  ) async {
    if (locales != null && locales.isNotEmpty &&
        locales.first != LocalizedApp.of(context).delegate.currentLocale) {
      if (Platform.isAndroid) {
        Restart.restartApp();
      }
    }
  }

In my case, I'm restarting the app natively for Android, due to problems trying to force-rebuild the entire widget tree. I'm using the restart_app plugin to do that.

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.

stale[bot] commented 9 months 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.