Éowyn
Éowyn

Reputation: 187

flutter_localizations package getting device language instead languageCode

I'm trying to show the apps content based on the user location. For this, I'm trying to get the languageCode from they country, but it's not working. The retrieved languageCode corresponds to the device language instead the country.

I'm doint this:

@override
  Widget build(BuildContext context) {
    return ScopedModel<UserModel>(
        model: UserModel(),
        child: ScopedModelDescendant<UserModel>(builder: (context, child, snapshot) {
          return MaterialApp(
            title: "APP",
            debugShowCheckedModeBanner: false,
            localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
            ],
            supportedLocales: allTranslations.supportedLocales(),
          );
        }));
  }

Where my supportedLocales are a list with all country/language code:

Iterable<Locale> supportedLocales() => [
    const Locale('ar','AE'),
    const Locale('ar','BH'),
    const Locale('ar','DZ'),
    const Locale('ar','EG'),
    const Locale('ar','IL'),
    const Locale('ar','IQ'),
    const Locale('ar','JO'),
    const Locale('ar','KW'),
    const Locale('ar','LB'),
    const Locale('ar','LY'),
    const Locale('ar','MA'),
    const Locale('ar','MR'),
    const Locale('ar','OM'),
    const Locale('ar','PS'),
    const Locale('ar','QA'),
...

I'm trying get the correct languageCode doing this:

String language = Localizations.localeOf(context).languageCode;

But the method always ignores the location and takes the device default language :(

E.g: my friend lives in Brazil and his device are with the en-CA language and he can't see the brazilian content becaus the language code always is 'en' instead 'pt'.

Upvotes: 0

Views: 4921

Answers (1)

Roslan Amir
Roslan Amir

Reputation: 1346

import dart.io;

...
final localeStr = Platform.localeName; // 'en-US'

Upvotes: 7

Related Questions