Web 应用程序中的区域设置始终为 'en' Flutter

Vin*_*nzo 5 locale flutter flutter-web

我的应用程序可以正确从 Android 和 iOS 获取设备区域设置,但不能从网络获取设备区域设置。ui.window.locale.languageCode我使用包获取网络区域设置语言代码ui.dart。在我的 Mac 中,我将系统语言设置为意大利语,时区设置为意大利,位置服务设置为启用,但当我在 Chrome 上运行我的应用程序时,它总是返回“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;
        },
...
Run Code Online (Sandbox Code Playgroud)

Flp*_*lpe 0

昨天我在 iOS 和 Web 上遇到了同样的问题。我用这个答案解决了iOS问题。我认为 Web 应用程序的问题将来会得到解决(Web 不被视为稳定的 Flutter 功能)。

\n\n

如果您确实需要在 iOS、Android 和 Web 上创建国际化应用程序,则可以使用此包

\n\n
FutureBuilder<Locale>(\n  future: DeviceLocale.getCurrentLocale(),\n  builder: (context, localeData) {\n    if(!localeData.hasData) return Container( color: Colors.white);\n    Locale locale = localeData.data;\n    return MaterialApp(\n      // localizationsDelegates, supportedLocales, ...\n\n      // \xc2\xab\xc2\xa0_\xc2\xa0\xc2\xbb is the locale detected by flutter\n      // You should use the locale detected by the library instead\n      localeResolutionCallback: (_, supportedLocales) {\n        for (var supportedLocaleLanguage in supportedLocales) {\n          if (supportedLocaleLanguage.languageCode == locale.languageCode) return supportedLocaleLanguage;\n        }\n\n        // If device not support with locale to get language code then default get first on from the list\n        return supportedLocales.first;\n      }\n    );\n  },\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n

您可以调用“Localizations.localeOf(context)”来获取 MaterialApp 小部件子级中的区域设置。

\n\n

该软件包目前似乎与桌面应用程序不兼容。

\n\n

我没有检查 Flutter Github 页面的“问题”选项卡是否已发出问题信号。也许我应该在今天晚上发出信号。

\n