Search code examples
flutterdartlocalization

flutter app does not load local device language


I implemented the Easy localization package on my app, and it works fine when i use the app radio button switch between the different languages. But the aim it is not to let users change language, it is to load local device language and set the app with the local language. I have changed device language in device settings, but when i re-open the app, language does not change.

Here the main.dart:

  void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();

  runApp(
    EasyLocalization(
      supportedLocales: [
        Locale('en', 'US'),
        Locale('it', 'IT'),
        Locale('fr', 'FR')
      ],
      path: 'assets/translations',
      // <-- change the path of the translation files
      fallbackLocale: Locale('en', 'US'),
      //assetLoader: CodegenLoader(),
      child: MyLangApp(),
    ),
  );
}

Here the MyLangApp:

  class MyLangApp extends StatefulWidget {
  const MyLangApp({Key? key}) : super(key: key);

  @override
  State<MyLangApp> createState() => _MyLangAppState();
}

class _MyLangAppState extends State<MyLangApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale,
      home: MainPage(),
    );
  }
}

Solution

  • If you want to get the devices locale use this:

    import 'dart:io';
    
    final String defaultLocale = Platform.localeName;
    

    see easy_localization documentation (https://pub.dev/packages/easy_localization) for changing the language: enter image description here

    Found this in the Flutter documentation(https://docs.flutter.dev/development/accessibility-and-localization/internationalization):

    Advanced locale definition Some languages with multiple variants require more than just a language code to properly differentiate.

    For example, fully differentiating all variants of Chinese requires specifying the language code, script code, and country code. This is due to the existence of simplified and traditional script, as well as regional differences in the way characters are written within the same script type.

    In order to fully express every variant of Chinese for the country codes CN, TW, and HK, the list of supported locales should include:

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

    This explicit full definition ensures that your app can distinguish between and provide the fully nuanced localized content to all combinations of these country codes. If a user’s preferred locale is not specified, then the closest match is used instead, which likely contains differences to what the user expects. Flutter only resolves to locales defined in supportedLocales. Flutter provides scriptCode-differentiated localized content for commonly used languages. See Localizations for information on how the supported locales and the preferred locales are resolved.

    Although Chinese is a primary example, other languages like French (fr_FR, fr_CA) should also be fully differentiated for more nuanced localization.

    Seems that you have to include them all or split the Platform.localeName into language and region and only use language then...

    Update

    try this:

    final deviceLocale= Locale(Platform.localeName) 
    
    await context.setLocale(deviceLocale.split('-')[0]);