-
-
Notifications
You must be signed in to change notification settings - Fork 336
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
Using Easy Localization witout context #210
Comments
@Overman775 Hi Overmann, did you find a workaround to use localization outside of widgets? |
@pumuckelo nope, have an idea, I’ll implement it later |
Isn't that already done? I see a |
That would be very helpful. |
@ncuillery even tr function doesn't need context but using tr outside the widget tree doesn't work |
Can you provide an example of your code and the errors you're getting? because the static tr() function works fine outside widget trees. |
but how does it get access to the language files |
context removed from tr() and plural() in PR #343 |
Can someone give an implementation example?
|
I finally got it to work. Someone should document the following somewhere, because it definitely is not currently. Step 1: Call Step 2: code executed in the isolate
Step 3: now you can actually translate
|
This solution worked but resetLocale saves the deviceLocale and inf deviceLocale wasn't in supportedLocale it will lead to an excpetion (ex: "assets/localizations/fr.json not found"). So instead of using |
I am trying to implement your code but getting error final Localization L = Localization.instance;
Future<void> loadTranslations() async {...}
Future main() async {
await loadTranslations();
// await EasyLocalization.ensureInitialized();
runApp(
App(store: store)
// EasyLocalization(
// path: 'assets/translations',
// assetLoader: CodegenLoader(),
// supportedLocales: [
// Locale('en', 'US'),
// Locale('hr', 'HR'),
// ],
// child: App(store: store),
// ),
//....
@override
Widget build(BuildContext context) {
context.resetLocale(); // error pointing here |
I had the same issue as @nkulic and I had to change the assetLoader because I use CodeGen.
and I could remove |
I ended up using a very recent and new package fast_i18n |
i have a similar problem and i tried this solution but im getting an error "MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)". await EasyLocalizationController.initEasyLocation(); when this line is work i get this error. how can i solve this? |
Thanks a lot for your help. It worked for me ❤️. I wanted to get working translations in the background thread when using awesome_notifications—adding this to execute in the background thread when the main thread is dead worked for me. await EasyLocalization.ensureInitialized();
final controller = EasyLocalizationController(
saveLocale: true,
fallbackLocale: const Locale('en'),
supportedLocales: const [
Locale('en'),
Locale('cs'),
],
assetLoader: const CodegenLoader(),
useOnlyLangCode: false,
useFallbackTranslations: true,
path: '/assets/translations/',
onLoadError: (FlutterError e) {},
);
//Load translations from assets
await controller.loadTranslations();
//load translations into exploitable data, kept in memory
Localization.load(controller.locale,
translations: controller.translations,
fallbackTranslations: controller.fallbackTranslations); And then this worked normally: In context: static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction) async {
SendPort? port = IsolateNameServer.lookupPortByName('main');
if (port != null) {
port.send(receivedAction);
} else {
if ((UniversalPlatform.isAndroid || UniversalPlatform.isIOS)) {
var quotesData = data;
if (await getLocaleIndex() == 1) {
quotesData = data_cz;
}
parseAllQuotes(quotesData).then((value) => quotesProvider = value);
}
await Hive.initFlutter();
Hive.registerAdapter(HabitAdapter());
Hive.registerAdapter(RewardAdapter());
Hive.registerAdapter(QuoteAdapter());
await Hive.openBox<Habit>(habitsBoxName);
await Hive.openBox(settingsBoxName);
await Hive.openBox<Reward>(rewardsBoxName);
await Hive.openBox<Quote>(favouriteQuotesBoxName);
await EasyLocalization.ensureInitialized();
final controller = EasyLocalizationController(
saveLocale: true,
fallbackLocale: const Locale('en'),
supportedLocales: const [
Locale('en'),
Locale('cs'),
],
assetLoader: const CodegenLoader(),
useOnlyLangCode: false,
useFallbackTranslations: true,
path: '/assets/translations/',
onLoadError: (FlutterError e) {},
);
//Load translations from assets
await controller.loadTranslations();
//load translations into exploitable data, kept in memory
Localization.load(controller.locale,
translations: controller.translations,
fallbackTranslations: controller.fallbackTranslations);
await AwesomeNotifications().initialize(
'resource://drawable/ic_stat_onesignal_default',
[
NotificationChannel(
channelKey: getChannelNameByType(NotificationSettings.habits),
channelName: getChannelNameByType(NotificationSettings.habits),
channelDescription:
LocaleKeys.notifications_habits_notifications.tr(),
defaultColor: primaryColor_74,
ledColor: Colors.blue,
enableLights: true,
importance: NotificationImportance.High,
channelShowBadge: true,
),
NotificationChannel(
channelKey: getChannelNameByType(NotificationSettings.quotes),
channelName: getChannelNameByType(NotificationSettings.quotes),
importance: NotificationImportance.Default,
channelDescription:
LocaleKeys.notifications_quotes_notifications.tr(),
defaultColor: secondaryColorGreen_43,
ledColor: Colors.green,
),
NotificationChannel(
channelKey: getChannelNameByType(NotificationSettings.rewards),
channelName: getChannelNameByType(NotificationSettings.rewards),
importance: NotificationImportance.Default,
channelDescription:
LocaleKeys.notifications_rewards_notifications.tr(),
defaultColor: Colors.black,
ledColor: Colors.green,
),
],
// debug: true,
);
await doneHabit(receivedAction);
}
} |
Looks like we don't need code here, but just add one of the solutions to the documentation and maybe to the example |
It's not necessary to always load the Import Pass the Get the translation: @bw-flagship it would be great if |
I see, thanks for the explanation! Feel free to open a pr :) |
Hi, I'm trying to use EasyLocalization with WorkManager. When I try method with using EasyLocalizationController looks like there is no such class. Is this solution up to date? |
@Mistic92 This solution is up-to-date, but I have no experience with work manager and can't tell you if this should work out of the box |
@bw-flagship it worked, I had to explicitly import from
as Android Studio didn't want to import from that location. Even now I'm getting warning but solution works
|
use this to solve |
No description provided.
The text was updated successfully, but these errors were encountered: