programmatically change language of flutter i18n apps doesn't work in iOS

删除回忆录丶 提交于 2020-12-06 04:16:26

问题


I used flutter_i18n plugin (Android Studio) to generate i18n.dart(class S) and S.of(context).locale_msg will return the locale string. The main code is shown below. Language should be changed programmatically by invoking onLocaleChange(locale) when click the button in HomePage. It works well in Android simulator, but won't change language in iOS simulator. Wonder what's wrong with my code?

class _PaperMoonAppState extends State<PaperMoonApp> {
  SpecifiedLocalizationDelegate _localeOverrideDelegate;

  void onLocaleChange(Locale locale) {
    setState(() {
      if (appVars.appConfig.changeLanguage(locale)) {
        _localeOverrideDelegate = new SpecifiedLocalizationDelegate(locale);
        appVars.saveConfig(); //print save config file...
      }
    });
  }

  @override
  void initState() {
    SpecifiedLocalizationDelegate.onLocaleChange = this.onLocaleChange;
    appVars.loadConfig().then((AppConfig _config) {
      appVars.appConfig = _config;

      setState(() {
        _localeOverrideDelegate =
            new SpecifiedLocalizationDelegate(appVars.appConfig.getLocale());
      });
    });
    _localeOverrideDelegate =
        new SpecifiedLocalizationDelegate(Locale('zh', ''));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    print(_localeOverrideDelegate.overriddenLocale);
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "Paper Moon",
      color: Colors.blueAccent,
      localizationsDelegates: [
        _localeOverrideDelegate,
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: const <Locale>[
        Locale("ja", ""),
        Locale("en", ""),
        Locale("zh", ""),
      ],
      localeResolutionCallback:
          S.delegate.resolution(fallback: _localeOverrideDelegate.overriddenLocale),
      home: HomePage(),
//      routes: _buildRoutes(),
    );
  }
}

Custom LocalizationDelegate:

class SpecifiedLocalizationDelegate
    extends LocalizationsDelegate<WidgetsLocalizations> {
  //class static vars:
  //onLocaleChange should be bind to MaterialApp function containing setState().
  static LocaleChangeCallback onLocaleChange;

  // for instance
  final Locale overriddenLocale;

  const SpecifiedLocalizationDelegate(this.overriddenLocale);

  @override
  bool isSupported(Locale locale) => overriddenLocale != null;



  @override
  Future<WidgetsLocalizations> load(Locale locale) =>
      S.delegate.load(overriddenLocale);

  @override
  bool shouldReload(SpecifiedLocalizationDelegate old) => true;
}

回答1:


Based on your code, the only thing that seems to be missing is this:

open ios/Runner/Info.plist and add:

    <key>CFBundleLocalizations</key>
    <array>
        <string>ja</string>
        <string>en</string>
        <string>zh</string>
    </array>

As far I as know, by now (march/2019), flutter doesn't yet add automatically the list of supported languages to this file.



来源:https://stackoverflow.com/questions/53674805/programmatically-change-language-of-flutter-i18n-apps-doesnt-work-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!