Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app does not load on device language #3

Open
moosalim opened this issue Feb 18, 2021 · 1 comment
Open

app does not load on device language #3

moosalim opened this issue Feb 18, 2021 · 1 comment

Comments

@moosalim
Copy link

Hello

THanks for the code,

I have noticed the the app does always load on english language. is there a way to make the app load based on device language when open for first time. Then the users can change the language if they want from the drop down menu

@podcoder
Copy link
Owner

podcoder commented Feb 24, 2021

Hello dear,
localeResolutionCallback should do the work.

  Locale _locale ;
 Widget build(BuildContext context) {
  
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: "Flutter Localization Demo",
        theme: ThemeData(primarySwatch: Colors.blue),
        locale: _locale,
        supportedLocales: [
          Locale("en", "US"),
          Locale("fa", "IR"),
          Locale("ar", "SA"),
          Locale("hi", "IN")
        ],
        localizationsDelegates: [
          DemoLocalization.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        localeResolutionCallback: (deviceLocal, supportedLocales) {
          for (var supportedLocale in supportedLocales) {
            if (supportedLocale.languageCode == deviceLocal.languageCode &&
                supportedLocale.countryCode == deviceLocal.countryCode) {
                _locale = deviceLocale // This should do the trick, but I haven't tested
              return supportedLocale;
            }
          }
          return supportedLocales.first;
        },
        onGenerateRoute: CustomRouter.generatedRoute,
        initialRoute: homeRoute,
      );
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants