Flutter get current time zone / code name is always en_US

Viewed 398

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?

2 Answers

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.

Related