Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter, Localizations without Country code, is it okay?

Tags:

flutter

When implementing localization, in the case of Spanish(Espanol), there are too many countries to use, so I am thinking of excluding the country code when initializing (the second parameter value). Is this no problem? The code is below. If you look at the Locale('es'), the argument value is missing at the back.

@override
  Widget build(BuildContext context) {
    return MaterialApp(
     supportedLocales: [            
        Locale('en', 'US'),
        Locale('sk', 'SK'),
        Locale('ja', 'JP'),
        Locale('es',),           
      ],
      localizationsDelegates: [           
        AppLocalizations.delegate,         
        GlobalMaterialLocalizations.delegate,         
        GlobalWidgetsLocalizations.delegate,
      ],
      localeResolutionCallback: (locale, supportedLocales) {
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode &&
              supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
          }
        }           
        return supportedLocales.first;
      },
    );
  }
like image 960
dontknowhy Avatar asked Nov 20 '25 20:11

dontknowhy


1 Answers

Yes, it is OK. You can use only the language code.

Locale.languageCode, Locale.scriptCode, and Locale.countryCode

Locale.languageCode and Locale.scriptCode only

Locale.languageCode and Locale.countryCode only

Locale.languageCode only

Locale.countryCode only when all preferred locales fail to match

Returns the first element of supportedLocales as a fallback

You can use like this:


// Full Chinese support for CN, TW, and HK
supportedLocales: [
  const Locale.fromSubtags(languageCode: 'zh'), // generic Chinese 'zh'
  const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), // generic simplified Chinese 'zh_Hans'
  const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant'), // generic traditional Chinese 'zh_Hant'
  const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN'), // 'zh_Hans_CN'
  const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant', countryCode: 'TW'), // 'zh_Hant_TW'
  const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant', countryCode: 'HK'), // 'zh_Hant_HK'
],

You can read more from the official document.

like image 107
Akif Avatar answered Nov 22 '25 09:11

Akif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!