flutterdartlocalizationflutter-intl

How to change the main localization file in Flutter intl


Initially, the main localization file is intl_en.arb How and where to change the main localization file to another one, for example intl_ru.arb


Solution

  • you can add languageCode on locale

    MaterialApp(
      .
      .
      .
      locale : Locale.fromSubtags(languageCode: 'ru')
    );
    

    complete example:

     String lang = '';
    
      _getLang() async {
        try {
          String data = await getLang();
          setState(() {
            lang = data;
          });
        } catch (e) {
          print(e);
        }
      }
    
      @override
      void initState() {
        super.initState();
        _getLang();
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          onGenerateRoute: RouteGenerator.generateRoute,
          localizationsDelegates: [
            S.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: S.delegate.supportedLocales,
          locale: lang == 'English'
              ? Locale.fromSubtags(languageCode: 'en')
              : Locale.fromSubtags(languageCode: 'ru'),
        );
      }
    
      Future<String> getLang() async {
        Future<SharedPreferences> _langPrefs = SharedPreferences.getInstance();
        SharedPreferences prefs = await _langPrefs;
        var lang = prefs.getString("lang");
        if (lang == null) {
          return null;
        }
        return lang;
      }