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

Library Doesn't Provide Way to get Current Locale in app without context #709

Open
ahmedsaleh210 opened this issue Sep 2, 2024 · 4 comments

Comments

@ahmedsaleh210
Copy link

The issue with the easy_localization package is that it does not provide a way to retrieve the current locale of the app without requiring the BuildContext. This limitation can be problematic in scenarios where the locale is needed in parts of the app where a BuildContext is not available, such as in background tasks or services.

For instance, when handling Firebase Cloud Messaging (FCM) in the background, you might need to customize the notification payload based on the user's current locale. Without a direct way to access the current locale, you might find it challenging to localize the content of notifications accurately.

This limitation means that to get the current locale in such scenarios, developers often need to implement workarounds, like saving the locale in shared preferences or another global state management solution that doesn't rely on the BuildContext.

@eastsss
Copy link

eastsss commented Sep 6, 2024

This is a huge problem for me as well..
Not only you need BuildContext, but you also have to implicitly depend on inherited widget (context.dependOnInheritedWidgetOfExactType<_EasyLocalizationProvider>();) which means you can't get current locale inside initState() calls, for example. I need something like context.read() from provider library.
Is there any way to get current locale from BuildContext without depending on inherited widget, so it doesn't cause any unnecessary rebuilds?

@moha-b
Copy link

moha-b commented Oct 12, 2024

You can use a navigatorKey to easily access context anywhere.
exa :

 static final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();
/// in your main.dart
return MaterialApp(
          title: 'Touring',
          navigatorKey: NavigationHelper.navigatorKey,
          debugShowCheckedModeBanner: false,
          onGenerateRoute: NavigationHelper.generateRoute,
          initialRoute: AppRoute.LOGIN,
)

this is the only configuration u want then u can do that

NavigationHelper.navigatorKey.currentContext!

and that's it u have the context

@ahmedsaleh210
Copy link
Author

ahmedsaleh210 commented Oct 15, 2024

You can use a navigatorKey to easily access context anywhere. exa :

 static final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();
/// in your main.dart
return MaterialApp(
          title: 'Touring',
          navigatorKey: NavigationHelper.navigatorKey,
          debugShowCheckedModeBanner: false,
          onGenerateRoute: NavigationHelper.generateRoute,
          initialRoute: AppRoute.LOGIN,
)

this is the only configuration u want then u can do that

NavigationHelper.navigatorKey.currentContext!

and that's it u have the context

You can use a navigatorKey to easily access context anywhere. exa :

 static final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();
/// in your main.dart
return MaterialApp(
          title: 'Touring',
          navigatorKey: NavigationHelper.navigatorKey,
          debugShowCheckedModeBanner: false,
          onGenerateRoute: NavigationHelper.generateRoute,
          initialRoute: AppRoute.LOGIN,
)

this is the only configuration u want then u can do that

NavigationHelper.navigatorKey.currentContext!

and that's it u have the context

This doesn't actually solve the issue. The problem occurs when the app is running in the background (like during background FCM handling), and in such cases, there is no active BuildContext at all. Even with a navigatorKey, the app doesn't have a valid context when it's not in the foreground, so I still can't access the locale this way.

One possible solution would be to save the current locale in SharedPreferences when the user changes it or when the app is launched. Then, in background tasks you can do operation what you want.

@abiudrn
Copy link

abiudrn commented Nov 18, 2024

You can set the current value as a Singleton and access it using the get_it package

Create a locale service

class LocaleService {
  Locale _currentLocale = const Locale('en');

  Locale get currentLocale => _currentLocale;

  void setLocale(Locale locale) {
    _currentLocale = locale;
  }
}

Then register the service when initializing your app e.g in main()

import 'package:get_it/get_it.dart';

final GetIt getIt = GetIt.instance.registerSingleton<LocaleService>(LocaleService());

Then use it anywhere in your app like so

import 'package:get_it/get_it.dart';

void printCurrentLocale() {
  Locale currentLocale = GetIt.I<LocaleService>().currentLocale;
  print('Current locale: ${currentLocale.languageCode}');
}

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

4 participants