Web 应用程序的语言环境始终是 'en' Flutter
Locale from web app is always 'en' Flutter
我的应用程序可以从 Android 和 iOS 正确获取设备区域设置,但不能从 Web 获取。我使用 ui.dart
包将网络语言环境代码设置为 ui.window.locale.languageCode
。在我的 Mac 中,我将系统语言设置为意大利语,时区设置为意大利,位置服务设置为启用,但是当我 运行 我的应用程序在 Chrome 上时,它总是 return 'en_'。
有什么我想念的吗?
这是 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;
},
...
我昨天在 iOS 和 web 上遇到了同样的问题。我用 this answer 解决了 iOS 问题。我想 web 应用程序的问题将在未来得到解决(web 不被视为稳定的 Flutter 功能)。
如果您确实需要在 iOS、Android 和网络上创建国际化应用程序,您可以使用 this package.
FutureBuilder<Locale>(
future: DeviceLocale.getCurrentLocale(),
builder: (context, localeData) {
if(!localeData.hasData) return Container( color: Colors.white);
Locale locale = localeData.data;
return MaterialApp(
// localizationsDelegates, supportedLocales, ...
// « _ » is the locale detected by flutter
// You should use the locale detected by the library instead
localeResolutionCallback: (_, supportedLocales) {
for (var supportedLocaleLanguage in supportedLocales) {
if (supportedLocaleLanguage.languageCode == locale.languageCode) return supportedLocaleLanguage;
}
// If device not support with locale to get language code then default get first on from the list
return supportedLocales.first;
}
);
},
);
您可以调用 "Localizations.localeOf(context)" 以获取 MaterialApp 小部件的子项中的语言环境。
这个包目前似乎与桌面应用程序不兼容。
我没有检查 Flutter Github 页面的 "issues" 选项卡是否已发出问题信号。也许我应该在今晚发出信号。
最终 master 分支上的区域设置按预期工作。
更新到 Channel master, 1.19.0-4.0.pre.73 解决了这个问题。
我的应用程序可以从 Android 和 iOS 正确获取设备区域设置,但不能从 Web 获取。我使用 ui.dart
包将网络语言环境代码设置为 ui.window.locale.languageCode
。在我的 Mac 中,我将系统语言设置为意大利语,时区设置为意大利,位置服务设置为启用,但是当我 运行 我的应用程序在 Chrome 上时,它总是 return 'en_'。
有什么我想念的吗?
这是 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;
},
...
我昨天在 iOS 和 web 上遇到了同样的问题。我用 this answer 解决了 iOS 问题。我想 web 应用程序的问题将在未来得到解决(web 不被视为稳定的 Flutter 功能)。
如果您确实需要在 iOS、Android 和网络上创建国际化应用程序,您可以使用 this package.
FutureBuilder<Locale>(
future: DeviceLocale.getCurrentLocale(),
builder: (context, localeData) {
if(!localeData.hasData) return Container( color: Colors.white);
Locale locale = localeData.data;
return MaterialApp(
// localizationsDelegates, supportedLocales, ...
// « _ » is the locale detected by flutter
// You should use the locale detected by the library instead
localeResolutionCallback: (_, supportedLocales) {
for (var supportedLocaleLanguage in supportedLocales) {
if (supportedLocaleLanguage.languageCode == locale.languageCode) return supportedLocaleLanguage;
}
// If device not support with locale to get language code then default get first on from the list
return supportedLocales.first;
}
);
},
);
您可以调用 "Localizations.localeOf(context)" 以获取 MaterialApp 小部件的子项中的语言环境。
这个包目前似乎与桌面应用程序不兼容。
我没有检查 Flutter Github 页面的 "issues" 选项卡是否已发出问题信号。也许我应该在今晚发出信号。
最终 master 分支上的区域设置按预期工作。 更新到 Channel master, 1.19.0-4.0.pre.73 解决了这个问题。