简体   繁体   English

如何在 Flutter 中以编程方式更改应用程序的语言?

[英]How to change the Application's language programmatically in Flutter?

Currently, I am using flutter_localizations in order to translate strings in an application.目前,我正在使用flutter_localizations来翻译应用程序中的字符串。 It works perfectly fine, but it is required to be able to switch localization from the app's settings by pressing a button or choosing the language from a dropdown list.它工作得非常好,但需要能够通过按下按钮或从下拉列表中选择语言来从应用程序的设置中切换本地化。 For this app, the BLoC design pattern is used.对于这个应用程序,使用了BLoC设计模式。

Can you please suggest any potential solution to this problem?您能否建议任何潜在的解决方案来解决这个问题? Thanks in advance:)提前致谢:)

For future readers, here's how you can achieve this:对于未来的读者,您可以通过以下方式实现这一目标:

The Locale is defined by the result returned from localeResolutionCallback function, within the MaterialApp widget. LocaleMaterialApp小部件中从localeResolutionCallback function 返回的结果定义。

In my case, I pass a final Locale defaultValue;就我而言,我传递了一个final Locale defaultValue; to my app root, the first widget in the tree (declared in runApp() ).到我的应用程序根目录,树中的第一个小部件(在runApp()中声明)。

At that point, I simply do this validation:那时,我只需进行以下验证:

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'package:intl/intl.dart';

import '../../localization.dart';
import '../views/home_view.dart';

class App extends StatelessWidget {
  App({Key key, this.defaultLanguage}) : super(key: key);
  final Locale defaultLanguage;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeView(),
      localizationsDelegates: [
        AppLocalization.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', null),
        const Locale('pl', null),
      ],
      localeResolutionCallback: (locale, supportedLocales) {
        if (defaultLanguage != null) {
          Intl.defaultLocale = defaultLanguage.toLanguageTag();
          return defaultLanguage;
        }
        if (locale == null) {
          Intl.defaultLocale = supportedLocales.first.toLanguageTag();
          return supportedLocales.first;
        }
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode) {
            Intl.defaultLocale = supportedLocale.toLanguageTag();
            return supportedLocale;
          }
        }
        Intl.defaultLocale = supportedLocales.first.toLanguageTag();
        return supportedLocales.first;
      },
    );
  }
}

If defaultLanguage was passed on by the bloc above, then it's used in the application, otherwise it does the standard validation to fetch the locale from the device.如果上面的 bloc 传递了defaultLanguage ,那么它会在应用程序中使用,否则它会执行标准验证以从设备中获取语言环境。

Bear in mind that you might need to protect the check by verifying if the defaultLanguage variable is of a supported locale.请记住,您可能需要通过验证defaultLanguage变量是否属于受支持的语言环境来保护检查。 In my case this is handled, so that's why I don't bother.在我的情况下,这是处理的,所以这就是我不打扰的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM