Narumi
Narumi

Reputation: 81

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

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;
}

Upvotes: 4

Views: 4480

Answers (2)

Eder Rodrigues
Eder Rodrigues

Reputation: 61

I'm using i18n_extensions, but with the same issue...

What worked for me, was use this:

supportedLocales: const <Locale>[
    const Locale('en'),
    const Locale('pt'),
],

Instead of this:

  supportedLocales: const <Locale>[
    const Locale('en', 'US'),
    const Locale('pt', 'BR'),
  ],

And then, my i18n.dart. file i've change from this:

extension Localization on String {
  static final _t = Translations.from("en_us", {
    passwordInput: {
      "en_us": "Password",
      "pt_br": "Senha",
    },
    searchingTitle: {
      "en_us": "Scanning for devices...",
      "pt_br": "Procurando dispositivos...",
    },
    ...

To this:

extension Localization on String {
  static final _t = Translations.from("en", {
    passwordInput: {
      "en": "Password",
      "pt": "Senha",
    },
    searchingTitle: {
      "en": "Scanning for devices...",
      "pt": "Procurando dispositivos...",
    },

It works fine for me.

Upvotes: 0

Feu
Feu

Reputation: 5780

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.

Upvotes: 4

Related Questions