ilteoood / flutter_i18n

I18n made easy, for Flutter!
MIT License
217 stars 57 forks source link

Language changes suddenly despite FlutterI18n.currentLocale(context)?.languageCode still returns desired language #195

Closed chamartt closed 3 months ago

chamartt commented 2 years ago

Hello,

I observe strange behavior for some users logged by FirebaseAuth.instance.currentUser using NetworkFileTranslationLoader. For the first 3 launchs, the behavior is correct, in this case app language set to French by default and FlutterI18n.currentLocale(context)?.languageCode returns fr as well.

But, when this logged user closes and reopens exactly 3 times his app, the app language is changing in English despite FlutterI18n.currentLocale(context)?.languageCode is still returning fr in the language menu.

The forcedLocale is also setted on fr on his launch (thanks to debugs logs I putted) so there is no reason that the app language is suddenly setted to English.

If this user use app version with FileTranslationLoader despite of NetworkFileTranslationLoader (so in local) OR if the user isn't logged in FirebaseAuth, the behavior is always excellent ! Only 2 users are concerned (we have more than 20k active users) and even with cache clearing and resinstalls, this strange behavior is always present, when they are logged, after the third app restarting.. The users doesn't call refresh method before the issue comes.

This is my code implementation, where in this case Platform.localeName.split('_')[0] is returning fr (thanks to debugs logs and when we debug FlutterI18n.currentLocale(context)?.languageCode is setted to fr):

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  Locale? locale;
  SharedPreferences.getInstance().then((sharePref) {
    if (sharePref.getString('savedLocale') == null)
      locale = Locale(Platform.localeName.split('_')[0]);
    else
      locale = Locale(sharePref.getString('savedLocale') as String);

    final FlutterI18nDelegate flutterI18nDelegate = FlutterI18nDelegate(
      translationLoader: NetworkFileTranslationLoader(
        useCountryCode: false,
        fallbackFile: 'en',
        forcedLocale: locale,
        baseUri: Uri.https("l-algo-de-paulo-8fb41.appspot.com.storage.googleapis.com", "translations"),
        decodeStrategies: [JsonDecodeStrategy()],
      ),
    );

    runApp(MyApp(flutterI18nDelegate));
  });
}

Then, flutterI18nDelegate is used in MaterialApp.localizationsDelegates like this:

localizationsDelegates: [
        widget.flutterI18nDelegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
]

As I said before, for the most part of users, everything is working well, the problem is only present when the user is logged in FirebaseAuth and using NetworkFileTranslationLoader. If he's logged but using FileTranslationLoader, everything works well. If he's not logged but using NetworkFileTranslationLoader, everythin works well also.

I don't know how could I get more informations to help you to understand this issue better, but not hesitate to ask me something !

Thanks a lot for your plugin and all your work !

ilteoood commented 3 months ago

Are you able to create a repro? I cannot reproduce this issue

chamartt commented 3 months ago

2 years after the ticket creation, the problem is not here anymore.

Thanks anyway !