aissat / easy_localization

Easy and Fast internationalizing your Flutter Apps
https://pub.dev/packages/easy_localization
MIT License
923 stars 331 forks source link

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

Open ahmedsaleh210 opened 2 months ago

ahmedsaleh210 commented 2 months ago

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

moha-b commented 1 month ago

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 commented 1 month ago

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.