Set the default language in a Flutter application
Asked Answered
H

5

16

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: '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: {


        },

      ),
    );

Can someone help me? Thanks in advance!

Hight answered 6/1, 2020 at 10:12 Comment(4)
Can you please clarify what your issue is. Is your default language not working? What language is your phone set to?Rucksack
My phone use english as default language. So, I cannot force my apps to use another language (Bahasa) even my phone use english as default language?Hight
Is your code not working at all, or are you simply always getting English as the default language? Have you implemented a language changer, or have you tried changing your phone's default language to see if your code is working?Rucksack
So I want my apps to use Bahasa as default language, for user after first install, no matter what language they set on their phone. I have implemented language change on my apps, again using easy_localization package, and it works perfectly. My apps still using english as default even Bahasa is set on my phone.Hight
H
5

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

Hight answered 25/11, 2020 at 4:6 Comment(2)
startLocale doesn't workBoxhaul
stop the App, run flutter pub get and run the App againGyasi
H
22

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

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'),
   ...
);

Houri answered 28/9, 2020 at 10:56 Comment(0)
H
5

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

Hight answered 25/11, 2020 at 4:6 Comment(2)
startLocale doesn't workBoxhaul
stop the App, run flutter pub get and run the App againGyasi
R
3

Only this works for me (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');
    }
    // The default locale
    return const Locale('id', 'ID');
  },
Rrhagia answered 10/7, 2022 at 20:15 Comment(0)
G
1

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/

Gale answered 6/1, 2020 at 10:57 Comment(2)
I use easy_localization package, should I still use flutter_localizations?Hight
I am not sure about the easy_localization package but I researched some of bit that. You can refer to this pub.dev/packages/easy_localization which will help you in your answer.Gale
M
1

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

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,)
    ),
Midshipman answered 13/6, 2021 at 12:56 Comment(1)
stop the App, run flutter pub get and run the App againGyasi

© 2022 - 2024 — McMap. All rights reserved.