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

Set the default language in a Flutter application

我正在尝试在我的应用程序中支持多种语言。我想在我的应用程序中支持两种语言:英语 (en) 和巴哈萨语 (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: {


        },

      ),
    );

有人可以帮助我吗?提前致谢!

您添加依赖项以使用flutter_localizations了吗? 要使用本地化包,您需要使用 flutter_localizations 包。为此,您必须将其作为依赖项添加到您的 pubspec.yaml 文件中,如下所示:

dependencies:
   flutter:
     sdk: flutter
   flutter_localizations:
     sdk: flutter

另外,您可以参考link,并检查您遇到困难的地方。另外,对于直截了当的回答,我深表歉意,因为我是新手,无法对您的回答发表评论。 https://www.didierboelens.com/2018/04/internationalization---make-an-flutter-application-multi-lingual/

您需要使用回调来设置默认语言。在您的 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: Locale('en', 'US'),
   ...
);

最新的 easy_localization(从版本 2.2.1 开始)包提供 startLocale 覆盖设备区域设置。

添加startLocale将您想要的语言设置为默认语言

EasyLocalization(
        supportedLocales: [Locale('en', 'US'),Locale('en', 'CA')],
        path: 'assets', // <-- change patch to your
        fallbackLocale: Locale('en', 'CA'),
        saveLocale: true,
        startLocale:  Locale('en', 'US'),
        child: MyApp(store: store,)
    ),