How I can set the default language in flutter using easy localization? - flutter

I'm trying to set the Arabic 'ar' the default language for my app, but it still launch in English.
Here's my code in the runApp in main.dart
runApp(
EasyLocalization(
supportedLocales: [
Locale('en'),
Locale('ar'),
],
fallbackLocale: Locale('en'),
assetLoader: CodegenLoader(),
path: 'assets/translations',
startLocale: Locale('ar'),
child: MyApp(),
),
);

You can read this, step by step :
https://blog.logrocket.com/internationalizing-app-flutter-easy-localization/
It is well explained.

Related

Changing SyncFusion Flutter calendar language

I am trying to include SyncFusion Flutter calendar in my Flutter app.
I need to change the locale for the calendar.
I have included following code just inside the MaterialApp widget
child: MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
SfGlobalLocalizations.delegate,
],
supportedLocales: [
const Locale('en'),
const Locale('zh'),
const Locale('he'),
const Locale('ru'),
const Locale('fr', 'BE'),
const Locale('fr', 'CA'),
const Locale('ja'),
const Locale('de'),
const Locale('hi'),
const Locale('ar'),
],
locale: const Locale('zh'),
I have also included:
import 'package:syncfusion_localizations/syncfusion_localizations.dart';
But both lines below are marked as error:
Undefined name 'GlobalMaterialLocalizations
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,

easy_localization restart the whole app every time I using hot reload

I am using easy_localization for translations in my Flutter app. everything is fine but while developing every time I use hot reload the whole app is restarted.
The console shows that easy_localization is reinitialized when hot reload:
and this is my code:
runApp(
ProviderScope(
overrides: [
prefsProvider.overrideWithValue(prefs),
],
child: EasyLocalization(
supportedLocales: const [Locale('en')],
path: 'assets/translations',
fallbackLocale: const Locale('en', 'US'),
child: const RavenApp(),
saveLocale: true,
),
),
);
Note: I tried to remove EasyLocalization and the issue despaired.

Flutter web directionality unexpected null value

I am using flutter web. I am using GetX package to manage my states & navigation. When the app starts everything is working fine and I am able to navigate to other pages without any problems.
The problem is when I press reload on my chrome browser the app breaks I get this error.
[GETX] Instance "GetMaterialController" has been created
[GETX] Instance "GetMaterialController" has been initialized
════════ Exception caught by widgets library ═══════════════════════════════════
The following TypeErrorImpl was thrown building Directionality(textDirection: ltr):
Unexpected null value.
The relevant error-causing widget was
Directionality
../…/root/get_material_app.dart:328
GetMaterialApp code:
GetMaterialApp(
title: 'My Web App',
debugShowCheckedModeBanner: false,
textDirection: TextDirection.ltr,
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: SignIn.routeName,
getPages: [
GetPage(
name: Home.routeName,
page: () => const Home(),
middlewares: [AuthMiddleware()],
),
GetPage(
name: SignIn.routeName,
page: () => SignIn(),
middlewares: [AuthMiddleware()],
),
GetPage(
name: SignUp.routeName,
page: () => SignUp(),
middlewares: [AuthMiddleware()],
),
],
);
I have even added textDirection: TextDirection.ltr. No errors of any type when I first run the app. The app breaks after I click reload.
I think I have a similar setup to you. I'm using GetX for route management.
I am using a static const String routeName = '/_login_page'; field in my Login page.
Here is my GetMaterialApp method:
return GetMaterialApp(
debugShowCheckedModeBanner: false,
textDirection: TextDirection.ltr,
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: LoginPage.routeName,
getPages: [
GetPage(
name: LoginPage.routeName,
page: () => const LoginPage(),
),
],
);
If I change the value of the route field in my Login page while the app is running and reload the browser, it consistently throws that error for me. If I then stop my project and rerun it, my works fine. I suspect the routeName field is being cached and when it's changed during runtime, it no longer matches.
Solution: If you rename your routeName field, stop and then rerun your project.

How to change initial language of flutter app?

I am trying to create Arabic flutter app and the default language of flutter app is English , I want the app to start with Arabic language , So how can I achieve this ?
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en'),
const Locale('ar'),
],
locale: const Locale('ar'),
);

Flutter get current time zone / code name is always en_US

I have a problem with my app...I can not get the correct current Locale of my app.
This:
print(Localizations.localeOf(context));
is always giving me:
en_US
but I am actually in Germany. I tried it on both Android and iPhone Simulator as well as on a real device. It is always giving me "en_US" :(
I create my MaterialApp like this:
MaterialApp(
title: 'ProjectX',
theme: ThemeData(
primarySwatch: ColorService.createMaterialColor(AppColors.secondary),
backgroundColor: AppColors.white,
scaffoldBackgroundColor: AppColors.white,
visualDensity: VisualDensity.adaptivePlatformDensity,
fontFamily: AppTextStyles.mainFont,
),
initialRoute: isOnline
? showOnboarding
? '/onboarding'
: '/'
: '/offline',
onGenerateRoute: AppRouter.generateRoute,
locale: Locale('de', 'DE'),
localizationsDelegates: [
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
),
Is this a known bug? What am I missing here?
Add supportedLocales named argument to MaterialApp as stated in Internationalization documentation.
MaterialApp(
title: 'ProjectX',
theme: ThemeData(
primarySwatch: ColorService.createMaterialColor(AppColors.secondary),
backgroundColor: AppColors.white,
scaffoldBackgroundColor: AppColors.white,
visualDensity: VisualDensity.adaptivePlatformDensity,
fontFamily: AppTextStyles.mainFont,
),
initialRoute: isOnline
? showOnboarding
? '/onboarding'
: '/'
: '/offline',
onGenerateRoute: AppRouter.generateRoute,
locale: Locale('de', 'DE'),
supportedLocales: [Locale('de', 'DE')],
localizationsDelegates: [
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
),
Is german the current language of the device?
In Locale, I think that it gets from keyboard language.
Like: if my keyboard language is US English, the locale will get en_US, even though I live in Brazil.