flutter_translate icon indicating copy to clipboard operation
flutter_translate copied to clipboard

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

Open bratan opened this issue 5 years ago • 12 comments

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

bratan avatar Nov 27 '19 13:11 bratan

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

diegoveloper avatar Nov 29 '19 20:11 diegoveloper

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

bratan avatar Nov 30 '19 09:11 bratan

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

doc-rj-celltrak avatar Mar 29 '20 19:03 doc-rj-celltrak

@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?

kuhnroyal avatar May 12 '20 16:05 kuhnroyal

@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

bratan avatar May 12 '20 16:05 bratan

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

kuhnroyal avatar May 12 '20 17:05 kuhnroyal

@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!

diegoveloper avatar Jun 25 '20 22:06 diegoveloper

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

doc-rj-celltrak avatar Aug 25 '20 17:08 doc-rj-celltrak

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

bratan avatar Aug 25 '20 18:08 bratan

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.

doc-rj-celltrak avatar Aug 25 '21 14:08 doc-rj-celltrak

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] avatar May 04 '22 14:05 stale[bot]

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] avatar Jan 20 '24 07:01 stale[bot]