Open ahmedsaleh210 opened 2 months ago
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?
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
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.
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}');
}
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.