-
-
Notifications
You must be signed in to change notification settings - Fork 335
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
Comments
This is a huge problem for me as well.. |
You can use a 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. |
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 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}');
} |
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.
The text was updated successfully, but these errors were encountered: