flutter - 在Flutter应用程序中设置默认语言

标签 flutter lang

我正在尝试在我的应用程序中支持多种语言。我想在我的应用程序中支持两种语言:英语(en)和Bahasa(id)。但是,我希望我的应用使用Bahasa作为默认语言。我尝试使用插件easy_localization做到这一点。

这是我的main.app文件中的一些代码

return EasyLocalizationProvider(
      data: data,
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: APP_NAME,

        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          //app-specific localization
          EasylocaLizationDelegate(
              locale: data.locale,
              path: 'assets/strings'
          ),
        ],
        navigatorKey: locator<NavigationService>().navigatorKey,

        supportedLocales: [ Locale('id', 'ID'), Locale('en', 'US')],
        locale: data.savedLocale,


        theme: ThemeData(
          primaryColor: KaskuColor.primary,
          accentColor: Color(0xFFCB0E00),
          fontFamily: PRIMARY_FONT_FAMILY,
          textTheme: TextTheme(
            headline: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
            title: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
            body1: TextStyle(fontSize: 14.0),
          ),
          primarySwatch: Colors.red,
          cursorColor: KaskuColor.primary,
          snackBarTheme: SnackBarThemeData(
            backgroundColor: KaskuColor.snackBarColor
          )
        ),

        home: Splashscreen(),
        routes: {


        },

      ),
    );

有人能帮我吗?提前致谢!

最佳答案

您需要使用回调来设置默认语言。在您的MaterialApp小部件中,添加localeListResolutionCallback,如下所示:-

MaterialApp(
   ...

   localeListResolutionCallback: (locales, supportedLocales) {

      print('device locales=$locales supported locales=$supportedLocales');

      for (Locale locale in locales) {
         // if device language is supported by the app,
         // just return it to set it as current app language
         if (supportedLocales.contains(locale)) {
            return locale;
         }
      }

      // if device language is not supported by the app,
      // the app will set it to english but return this to set to Bahasa instead
      return Locale('id', 'ID');
   },

   supportedLocales: [Locale('id', 'ID'), Locale('en', 'US')],
   locale: localConfig.locale,
   ...
);

关于flutter - 在Flutter应用程序中设置默认语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610200/

相关文章:

html - 如何通过在 html 中设置语言来使浏览器自动格式化数字?

firebase - 为什么Firestore数据赋予Class 'QueryDocumentSnapshot'没有实例方法 '[]'。错误?

Flutter Padding EdgeInsets fromLTRB 将内容推到底部

ios - 我错误地将我的 iOS 应用程序(flutter) 连接到了错误的 firebase 项目,我可以更改 firebase 项目吗?

flutter - StatefulWidget 中的 State 对象何时销毁?

android - 无法构建 Flutter Android Build : Received status code 502 from server: Bad Gateway

html - 如何在外部 CSS 文件 (.css) 中设置 <html> 的 lang 全局属性

php - 如何将字符串和变量连接成常量名称?

html - <html lang ="de-DE">是否等于&lt;meta http-equiv ="language"content ="DE">?