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

huangapple go评论72阅读模式
英文:

Set the default language in a Flutter application

问题

我正在尝试在我的应用程序中支持多种语言。我想在我的应用程序中支持两种语言:英语(en)和巴哈萨(id)。但我希望我的应用程序默认使用巴哈萨作为语言。我尝试使用插件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: {

    },

  ),
);

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

英文:

I am trying to support multiple languages in my apps. I want to support two languages in my apps: English (en) and Bahasa (id). But, I want my apps to use Bahasa as the default language. I have tried to do this using the plugin easy_localization.

Here is some code from my main.app file

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

        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          //app-specific localization
          EasylocaLizationDelegate(
              locale: data.locale,
              path: &#39;assets/strings&#39;
          ),
        ],
        navigatorKey: locator&lt;NavigationService&gt;().navigatorKey,

        supportedLocales: [ Locale(&#39;id&#39;, &#39;ID&#39;), Locale(&#39;en&#39;, &#39;US&#39;)],
        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: {
          

        },

      ),
    );

Can someone help me? Thanks in advance!

答案1

得分: 21

你需要使用回调函数来设置默认语言。在你的MaterialApp小部件中,添加以下的localeListResolutionCallback:-

MaterialApp(
   ...

   localeListResolutionCallback: (locales, supportedLocales) {

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

      for (Locale locale in locales) {
         // 如果设备语言受应用支持,
         // 直接返回它以将其设置为当前应用程序语言
         if (supportedLocales.contains(locale)) {
            return locale;
         }
      }

      // 如果设备语言不受应用支持,
      // 应用程序将其设置为英语,但返回这个以设置为印尼语
      return Locale('id', 'ID');
   },

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

英文:

You need to use a callback to set a default language. In your MaterialApp widget add localeListResolutionCallback as following:-

MaterialApp(
   ...

   localeListResolutionCallback: (locales, supportedLocales) {

      print(&#39;device locales=$locales supported locales=$supportedLocales&#39;);

      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(&#39;id&#39;, &#39;ID&#39;);
   },

   supportedLocales: [Locale(&#39;id&#39;, &#39;ID&#39;), Locale(&#39;en&#39;, &#39;US&#39;)],
   locale: Locale(&#39;en&#39;, &#39;US&#39;),
   ...
);

答案2

得分: 4

最新版本的easy_localization包提供了startLocale选项,该选项可以覆盖设备的区域设置。

英文:

Latest easy_localization (from version 2.2.1) package provide startLocale which overrides device locale.

答案3

得分: 2

只有这个对我有效 (Flutter 3.0.4, Dart 2.17.5):

  supportedLocales: const [
    Locale('id', 'ID'),
    Locale('en', 'US'),
  ],
  localeListResolutionCallback: (allLocales, supportedLocales) {
    final locale = allLocales?.first.languageCode;
    if (locale == 'en') {
      return const Locale('en', 'US');
    }
    // 默认区域设置
    return const Locale('id', 'ID');
  },
英文:

Only this works for me (Flutter 3.0.4, Dart 2.17.5):

  supportedLocales: const [
    Locale(&#39;id&#39;, &#39;ID&#39;),
    Locale(&#39;en&#39;, &#39;US&#39;),
  ],
  localeListResolutionCallback: (allLocales, supportedLocales) {
    final locale = allLocales?.first.languageCode;
    if (locale == &#39;en&#39;) {
      return const Locale(&#39;en&#39;, &#39;US&#39;);
    }
    // The default locale
    return const Locale(&#39;id&#39;, &#39;ID&#39;);
  },

答案4

得分: 1

你是否已经添加了使用flutter_localizations的依赖项?
要使用本地化包,您需要使用flutter_localizations包。为此,您需要将其添加为您的pubspec.yaml文件的依赖项,如下所示:

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

此外,您可以参考以下链接,查看您在哪里遇到困难。同时,对于我直接回答的回复表示抱歉,因为我是新手,无法在您的答案上发表评论。
https://www.didierboelens.com/2018/04/internationalization---make-an-flutter-application-multi-lingual/

英文:

Have you added the dependencies to use the flutter_localizations?
To use the localization package, you will need to use the flutter_localizations package. To do so, you will have to add it as a dependency to your pubspec.yaml file as follows:

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

Also, you can refer to the link and check it where you are having difficulties. Also, apologies for the straight forward answer as I am new to this I was not able to comment on your answer.
https://www.didierboelens.com/2018/04/internationalization---make-an-flutter-application-multi-lingual/

答案5

得分: 1

添加 startLocale 以设置您希望作为默认语言的语言。

EasyLocalization(
supportedLocales: [Locale('en', 'US'), Locale('en', 'CA')],
path: 'assets', // <-- 将路径更改为您的
fallbackLocale: Locale('en', 'CA'),
saveLocale: true,
startLocale: Locale('en', 'US'),
child: MyApp(store: store,)
),

英文:

add startLocale to set the language you want as a default language

EasyLocalization(
        supportedLocales: [Locale(&#39;en&#39;, &#39;US&#39;),Locale(&#39;en&#39;, &#39;CA&#39;)],
        path: &#39;assets&#39;, // &lt;-- change patch to your
        fallbackLocale: Locale(&#39;en&#39;, &#39;CA&#39;),
        saveLocale: true,
        startLocale:  Locale(&#39;en&#39;, &#39;US&#39;),
        child: MyApp(store: store,)
    ),

huangapple
  • 本文由 发表于 2020年1月6日 18:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定