0

最初,主本地化文件是intl_en.arb 如何以及在何处将主本地化文件更改为另一个,例如intl_ru.arb

4

1 回答 1

1

你可以languageCode添加locale

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

完整的例子:

 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;
  }
于 2021-04-03T18:53:53.910 回答