Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the default language in a Flutter application

Tags:

flutter

lang

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!

like image 721
fajar ainul Avatar asked Jan 06 '20 10:01

fajar ainul


People also ask

How do you add a language on Flutter app?

By default, Flutter only provides US English localizations. To add support for other languages, an application must specify additional MaterialApp (or CupertinoApp ) properties, and include a package called flutter_localizations . As of November 2020, this package supports 78 languages.

Which languages are used in flutter?

Flutter apps are written in the Dart language and make use of many of the language's more advanced features. While writing and debugging an application, Flutter runs in the Dart virtual machine, which features a just-in-time execution engine.

How many languages does flutter have?

This package supports 77 languages.


1 Answers

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

like image 85
bikram Avatar answered Sep 21 '22 20:09

bikram