easy_localization
easy_localization copied to clipboard
Language not changing
I've went through other issues and this isn't like #370.
Application changes the direction of the language and the locale gets changed but the words of the application don't get changed they remain on the same language until I restart the application myself, it was working fine on the ^2.3.3 version before null safety and it changed instantly now I don't know what happened!
I've tried it on a normal application, using provider, and using bloc and it's the same issue
I got same issue too. In my case I want to change language from model class by passing context from StatefulWidget to Model class to call context.locale. And it doesn't work.
Please more details ??!
Please test with MaterialApp.router, you will see the language not refresh in UI when we change the language. It works fine in MaterialApp but not MaterialApp.router
The solution that worked for me is the following:
My app has a bottom navigation bar where the tabs are in IndexedStack I was supplying the IndexedStack children list with const
children: [
const ProfilePage();
const HomePage();
]
The fix is to remove the const as follows:
children: [
// ignore: prefer_const_constructors
ProfilePage();
// ignore: prefer_const_constructors
HomePage();
]
Every other widget inside HomePage or ProfilePage can be const.
The solution that worked for me is the following: My app has a bottom navigation bar where the tabs are in
IndexedStackI was supplying theIndexedStackchildren list withconstchildren: [ const ProfilePage(); const HomePage(); ]The fix is to remove the
constas follows:children: [ // ignore: prefer_const_constructors ProfilePage(); // ignore: prefer_const_constructors HomePage(); ]Every other widget inside HomePage or ProfilePage can be
const.
Yes you are awesome, saved my day, thank you.
The solution that worked for me is the following: My app has a bottom navigation bar where the tabs are in
IndexedStackI was supplying theIndexedStackchildren list withconstchildren: [ const ProfilePage(); const HomePage(); ]The fix is to remove the
constas follows:children: [ // ignore: prefer_const_constructors ProfilePage(); // ignore: prefer_const_constructors HomePage(); ]Every other widget inside HomePage or ProfilePage can be
const.
Do we have better workaround so we don't need to remove the const keyword? Yes, i am aware of what const does but it is kind of hard to slice the widget if it is so deep in the widget tree. With current default linter settings, it's kind of odd to ignore it manually as it is not intuitive at least for me.
Easy Localization uses bad practice - it doesn't utilize BuildContext. That's why your translations will never be updated if they are const.
The solution that worked for me is the following: My app has a bottom navigation bar where the tabs are in
IndexedStackI was supplying theIndexedStackchildren list withconstchildren: [ const ProfilePage(); const HomePage(); ]The fix is to remove the
constas follows:children: [ // ignore: prefer_const_constructors ProfilePage(); // ignore: prefer_const_constructors HomePage(); ]Every other widget inside HomePage or ProfilePage can be
const.Do we have better workaround so we don't need to remove the
constkeyword? Yes, i am aware of whatconstdoes but it is kind of hard to slice the widget if it is so deep in the widget tree. With current default linter settings, it's kind of odd to ignore it manually as it is not intuitive at least for me.
don't forget to delete the 'const' on MaterialApp child/home widget if it exists
Same issue context.setLocale does not effect. Please help.
To update the translations, since Easy_Localization doesn't use the context, just update the context of the EasyLocalization instance.
With this solution I managed to fix this problem.
My Code:
Padding(
padding: const EdgeInsets.all(18.0),
child: DropdownButton(
items: context.supportedLocales
.map(
(e) => DropdownMenuItem(
value: e,
child: Text(
context.localizables.localeTitle(e.languageCode),
style: context.styles.h2Lato,
),
),
)
.toList(),
value: context.locale,
onChanged: (value) => value != null ? context.setLocale(value) : null,
),
),
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
extension LanguageLoader on BuildContext {
Localizable get localizables => Localizable(this);
}
class Localizable {
final BuildContext context;
Localizable(this.context);
String get appTitle => ltr('appTitle');
String get yes => ltr('yes');
String get no => ltr('no');
String get quoteAnime => ltr('quoteAnime');
String darkModeTitle(bool enabled) => ltr('darkModeTitle', args: [enabled ? yes : no]);
String localeTitle(String locale) => ltr('locale.$locale');
String ltr(
String key, {
List<String>? args,
Map<String, String>? namedArgs,
String? gender,
}) {
EasyLocalization.of(context);
return tr(key, args: args, namedArgs: namedArgs, gender: gender);
}
}
Duplicate of #552