I have my app that is correctly getting device locale from Android and iOS, but not from web. I get the web locale language code as ui.window.locale.languageCode using ui.dart package. In my Mac I set the system language to Italian, time zone is set to Italy and Location services set to enabled but when I run my app on Chrome it always return 'en_'.
Is there something I am missing to do?
This is the code in main() :
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Locale myLocale = Localizations.localeOf(context);
return MaterialApp(
title: '',
color: Colors.red,
localizationsDelegates: [
const AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'),
const Locale('it', 'IT')
// const Locale('es', 'ES'),
],
localeResolutionCallback:
(Locale locale, Iterable<Locale> supportedLocales) {
for (Locale supportedLocale in supportedLocales) {
if (kIsWeb) {
Locale webLocale = Locale(ui.window.locale.languageCode, '');
print('system locale is ${webLocale}');
return webLocale;
} else if (supportedLocale.languageCode == locale.languageCode ||
supportedLocale.countryCode == locale.countryCode) {
print('device Locale is $locale');
return supportedLocale;
}
}
return supportedLocales.first;
},
...