Home > Software design >  Switching locale
Switching locale

Time:07-09

I followed the guide on localization and it is working great. But I would like the user to be able to change to a locale in the app that is different from the local of the device. Can I somehow just set the locale for the app and have the entire app switch to using this locale?

I tried experimenting with "localeResolutionCallback" but without luck. Any ideas on how I can do this?

Thank you
Søren

CodePudding user response:

The is no need to use localeResolutionCallback.

You can simply specify the locale like this:

Locale _locale = Locale(Platform.localeName);
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            locale: _locale,
            home: const WelcomePage(),
          );
        }

Then you can provide the user a list of Locales to choose and create a function like this to update the app Locale:

setLocale(Locale locale) {
setState(() {
  _locale = locale;
});

}

To call setLocale(), you can use provider or findAncestorStateOfType to expose the initial Widget (it should be stateful)

Best regards

  • Related