0

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'.

3
  • Did you try to use the country code instead ? .countryCode instead of .languageCode Commented Apr 4, 2019 at 14:24
  • Yes, always comes 'us' :( Commented Apr 4, 2019 at 15:25
  • Oww. Sorry I don't know how to help you :( Commented Apr 4, 2019 at 15:27

1 Answer 1

7
import dart.io;

...
final localeStr = Platform.localeName; // 'en-US'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.