Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working on iOS in release #47

Open
PsyKoMari opened this issue Nov 14, 2019 · 8 comments
Open

Not working on iOS in release #47

PsyKoMari opened this issue Nov 14, 2019 · 8 comments

Comments

@PsyKoMari
Copy link

Hello, I used this plugin to generate my json files for the translations.
It was super simple and fast, worked immediately on both Android and iOS.
Now I want to test it in release using flutter run --release, on Android the localization works perfectly, but on iOS only works in debug, in release mode the locale is always en_US (my fallback language).

Am I missing something? Someone has the same problem?

I followed all the the steps in the github page/vs code plugin page.

This is my flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel dev, v1.10.15, on Mac OS X 10.15.1 19B88, locale it-IT)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 11.2.1)
[✓] Android Studio (version 3.5)
[✓] VS Code (version 1.40.0)
[✓] Connected device (1 available)

@arnemolland
Copy link

Have you added your supported locales to your Info.plist?

@PsyKoMari
Copy link
Author

Yes, I followed the whole guide and added

	<key>CFBundleLocalizations</key>
	<array>
		<string>en</string>
		<string>it</string>
	</array>

to my Runner/Info.plist

@PsyKoMari
Copy link
Author

Another information:

I'm trying to get this working and I noticed that if I run from command palette
Start debugging or Start without debugging the translations works.
If I run the app with flutter run or flutter run --release doesn't work. So this presents also with flutter run command... Any idea?

@ceeyang
Copy link

ceeyang commented Dec 6, 2019

Something error with me. I show you my code and the problem ↓↓↓↓↓↓

Development environment:

➜  flutter_template flutter doctor -v
[✓] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.15.1 19B88, locale en-CN)
    • Flutter version 1.9.1+hotfix.6 at /Users/cy/development/flutter
    • Framework revision 68587a0916 (3 months ago), 2019-09-13 19:46:58 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/cy/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.2.1, Build version 11B500
    • CocoaPods version 1.8.4

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 38.2.3
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.40.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.7.0

[✓] Connected device (1 available)
    • iPhone 7 Plus • e38bb612c85e3dca0e22f1e6b9cc3899b68b5a46 • ios • iOS 13.2.3

• No issues found!
➜  flutter_template 

This is my test code:

import 'package:flutter/material.dart';
import 'package:flutter_template/generated/i18n.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() => runApp(App());

class App extends StatefulWidget {
  App({Key key}) : super(key: key);

  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  final i18n = I18n.delegate;

  @override
  void initState() {
    super.initState();
    I18n.onLocaleChanged = onLocaleChange;
  }

  void onLocaleChange(Locale locale) {
    setState(() {
      I18n.locale = locale;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Template",
      localizationsDelegates: [
        i18n,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate // <-- needed for iOS
      ],
      supportedLocales: i18n.supportedLocales,
      localeResolutionCallback: i18n.resolution(fallback: new Locale("zh", "CN")),
      
      // flutter run --debug:  throw error
      // flutter run --release: throw error
      //
      // when I use I18n.of(context).homePageTitle with this way, It will throw error with message:
      // 
      //   NoSuchMethodError: The getter 'HomePageTitle' was called on null. 
      //   Receiver: null
      //   tried calling: homePageTitle
      // 
      // I'm sure I had made the homePageTitle's value and it's not null.
      //home: MyHomePage(title: I18n.of(context).homePageTitle),
      home: MyHomePage(title: "I18n Test"), 

    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  int _counter = 0;


  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        leading: IconButton(icon: Icon(Icons.settings),onPressed: () {
          I18n.onLocaleChanged(Locale('zh'));
        },),
        actions: <Widget>[
          FlatButton(child: Text('en-US'),textColor: Colors.white,onPressed: (){
            I18n.onLocaleChanged(Locale('en'));
          }),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
             // flutter run --debug: work well
             // flutter run --release: work well
              I18n.of(context).greetTo("name") + " " + I18n.of(context).homePageTitle
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

I am a new flutter developer.
This project doesn't have an example.
I don't what's wrong.

@idearibosome
Copy link

I also have exactly the same problem.

I figure it out and the reason is because Flutter calls load function of GeneratedLocalizationsDelegate twice, with the en-US locale for the first call and then the actual device locale for the second call.

However, the generated i18n.dart code does not update locale to the last one.
A possible workaround is to change a line in GeneratedLocalizationsDelegate.load() as follows:
Before: I18n._locale ??= locale;
After: I18n._locale = locale;

@NuclearGear
Copy link

I also have exactly the same problem.

I figure it out and the reason is because Flutter calls load function of GeneratedLocalizationsDelegate twice, with the en-US locale for the first call and then the actual device locale for the second call.

However, the generated i18n.dart code does not update locale to the last one.
A possible workaround is to change a line in GeneratedLocalizationsDelegate.load() as follows:
Before: I18n._locale ??= locale;
After: I18n._locale = locale;

Hi @idearibosome , the patch is working in iOS but not in android.
Do you find/solve this problem?
Thanks!

@idearibosome
Copy link

@NuclearGear I have this issue only on iOS. On Android, it works well without that patch.

@kubasaw
Copy link

kubasaw commented Jan 31, 2020

I also have exactly the same problem.
I figure it out and the reason is because Flutter calls load function of GeneratedLocalizationsDelegate twice, with the en-US locale for the first call and then the actual device locale for the second call.
However, the generated i18n.dart code does not update locale to the last one.
A possible workaround is to change a line in GeneratedLocalizationsDelegate.load() as follows:
Before: I18n._locale ??= locale;
After: I18n._locale = locale;

Hi @idearibosome , the patch is working in iOS but not in android.
Do you find/solve this problem?
Thanks!

Hi!

I think that is a bug. Two lines after I18n._locale ??= locale; there are another two checks for: I18n._locale != null, what IMO does not make sense.
To confirm that, can you check, if Android app is retranslated when you go to the device Settings, change Language and return to the app without restarting it? IMO, in this case nothing happens, but framework should retranslate app.

I'm able to do pull request quickly but I don't know if I'm right with my thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants