I have integrated flutter localizations dependent of system language, works very well.
But I want to "force" language programmatically, without matters on system language.
My app root:
return GetMaterialApp(
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('sr', ''),
Locale('en', ''),
],
My lang config file:
arb-dir: lib/l10n
template-arb-file: app_sr.arb
output-localization-file: app_localizations.dart
Any every next new entering into the app will "keep" the user choose preferred language.
Thank you!
You can add the locale attribute to your MaterialApp, like that:
// Content from your MaterialApp
locale: const Locale('sr'),
// More content from your MaterialApp
If you need change in another part of your app, you can change de Locale in your AppLocalizations class.
Related
my "(" ")" get flipped when I try to write text with Hebrew (rtl).
I look up in the internet but its don't work for me like:
locale: const Locale('he', 'IL'),
localizationsDelegates: const [
GlobalCupertinoLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('he', 'IL'),
const Locale('en', '')
],
.
.
.
and:
return Directionality(textDirection: TextDirection.rtl, child: TabsScreen());
after a lot of test I find that the problem trigger when the phone settings is on English or another language but if the settings on Hebrew is work.
You can use the direction in text field to switch between rtl and ltr
Text(
direction : selectedLanguage == 'hebrew' ? TextDirection.rtl:TextDirection.ltr,
)
rebuild the app after do something like change the theme or change the current language , iam using the ( easy_localization: ^3.0.0 ) package to change the lang
EasyLocalization(
child: MyApp(currentMode: currentMode),
supportedLocales: const [
Locale('ar'),
Locale('en'),
],
path: 'assets/translations',
startLocale: const Locale('ar'),
assetLoader: const CodegenLoader(),
),
If you're looking for the solution to this problem, this one works perfectly(tested) !
Add the package phoenix from here
Wrap your main widget with Phoenix
void main() {
runApp(
Phoenix(
child: App(),
),
);
}
Call rebirth method anywhere in your app like so Phoenix.rebirth(context);
Or make use of state management solutions like riverpod.
You place your settings in a provider and watch it on the pages that should change when the state updates.
When working with this package for localization, and base translation done in EN, I want my 2nd language to show keys if they are present in 2nd language json file, but if they are not, to consume en.json equivalent value. Issue is that by default this package shows key that doesn't exist in JSON file, instead of fallback translation file value. Is there a way to override this?
Usage of the plugin in main.dart file
runApp(
EasyLocalization(
child: MyApp(),
useOnlyLangCode: true,
fallbackLocale: Locale('en'),
supportedLocales: [
Locale('en'),
Locale('es'),
],
path: 'lang',
),
);
And when I want to translate a key that exists in en.json, but doesn't exist in es.json file it looks like this:
tr('appTitle');
Expected result would be "Hello world", but I get "appTitle" on the screen.
Old question, but since I just ran into the same problem:
There is now(?) a parameter "useFallbackTranslations" that you have to set to true. Indeed a bit counterintuitive, because one would assume that setting fallbackLocale should be enough.
EasyLocalization(
fallbackLocale: const Locale('en'),
supportedLocales: const [
Locale('en'),
Locale('es'),
Locale('de'),
],
useOnlyLangCode: true,
useFallbackTranslations: true, // <------
path: 'assets/i18n',
child: const MyApp(),
))
This is the EasyLocalization settings in runApp:
runApp(
EasyLocalization(
useOnlyLangCode: true,
useFallbackTranslations: true,
path: LanguageManager.langAssetPath,
supportedLocales: LanguageManager.instance.supportedLocales,
startLocale: LanguageManager.instance.dlLocale,
fallbackLocale: LanguageManager.instance.dlLocale,
child: const App(),
),
);
And the separated LanguageManager class
class LanguageManager {
LanguageManager._init();
static LanguageManager get instance => _instance ??= LanguageManager._init();
static LanguageManager? _instance;
static const langAssetPath = 'assets/translations';
final dlLocale = const Locale('en', 'US'); // necessary
final enLocale = const Locale('en');
final trLocale = const Locale('tr');
List<Locale> get supportedLocales => [dlLocale, enLocale, trLocale];
// constants
static const supportedLanguages = ['en', 'tr'];
Locale getCurrentLocale() {
return supportedLanguages.contains(getCurrentLang)
? Locale(getCurrentLang)
: const Locale('en');
}
String get getCurrentLang => Platform.localeName.substring(0, 2);
}
Finally assets/translations folder must en-US.json, en.json and tr.json
Don't forget generating
PS: This solution is from Veli Bacik in Turkey
I want to create an application that the intro app of my app to be LTR and other parts to be RTL. I found some code for RTL:
localizationsDelegates: [
GlobalCupertinoLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale("fa", "IR"), // OR Locale('ar', 'AE') OR Other RTL locales
],
locale: Locale("fa", "IR")
But it is used for all parts, not some parts, please help me.
I used this package for my app intro
I am having problems switching the language in a flutter-web application.
I added the required dependencies to the package.yml.
dependencies:
flutter:
sdk: flutter
intl: ^0.16.1
flutter_localizations:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
intl_translation: ^0.17.9
Then I initialized the intl stuff in the MaterialApp.
[...]
child: MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
S.delegate,
],
supportedLocales: S.delegate.supportedLocales,
[...]
Here is the code I expect to change the locale.
Text(S.of(context).language),
RaisedButton(
child: Text("deutsch"),
onPressed: () {
S.load(Locale('de'));
},
),
RaisedButton(
child: Text("englisch"),
onPressed: () {
S.load(Locale('en'));
},
Here are two arb files containing the translation.
{
"language": "Deutsch"
}
{
"language": "English"
}
Everything compiles and I can access the S.of(context) classes. I am sure I missed something. Since the switching of the language doesn't work either if I start in the Android Emulator.
Switching the system locale in the emulator changes the language.
I would appreciate any help or hints.