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

No fallback? #2

Closed
JCKodel opened this issue Nov 4, 2019 · 5 comments
Closed

No fallback? #2

JCKodel opened this issue Nov 4, 2019 · 5 comments

Comments

@JCKodel
Copy link

JCKodel commented Nov 4, 2019

Considering:

static var t = Translations("pt") +
    {
      "pt": "ENTRAR COM GOOGLE",
      "en": "ENTER WITH GOOGLE",
      "es": "INICIA COM GOOGLE",
    };

Results in:

➜ There are no translations in 'pt_br' for "ENTRAR COM GOOGLE".

I was expecting something like pt_pt, pt_ao, pt_br too fallback to pt (I'm lazy and don't want to translate every variant of portuguese, english and spanish)

BTW, my phone is en_US (don't know why it is trying to find pt_br)

@marcglasberg
Copy link
Owner

It should fallback, yes. It should try, in order:

  1. The exact locale (for example, if the device is "pt_br" and it finds "pt_br").
  2. Then the general language (for example, if the device is "pt_br" and it finds "pt").
  3. Then any locale with that language (for example, if the device is "pt_br" and it finds "pt_ao").

But, at the moment, the locale in the constructor should not be general. Try Translations("pt_br") instead of Translations("pt").

In other words, try this:

static var t = Translations("pt_br") +
  {
  "pt_br": "ENTRAR COM GOOGLE",
  "en": "ENTER WITH GOOGLE",
  "es": "INICIA COM GOOGLE",
  };

If it doesn't work, please let me know.

But even if it works, you are right that it should accept Translations("pt"). In a future version I will change that. I just need Dart 2.6 to go to the stable channel so that I can work in this package again.

Also, have a look at https://github.com/marcglasberg/i18n_extension/blob/master/test/i18n_extension_test.dart and see that it tests the fallbacks. If you can, try to modify some of the tests there to create a failing test to help me fix the problem.

@JCKodel
Copy link
Author

JCKodel commented Nov 5, 2019

Phone: English, United States

static var t = Translations("pt_br") +
    {
      "pt": "ENTRAR COM GOOGLE",
      "en": "ENTER WITH GOOGLE",
      "es": "INICIA COM GOOGLE",
    };

Error: `No default translation for 'pt_br'. But I don't want just br, I want all portuguese variants!

static var t = Translations("pt_br") +
    {
      "pt_br": "ENTRAR COM GOOGLE",
      "en": "ENTER WITH GOOGLE",
      "es": "INICIA COM GOOGLE",
    };

Button remains in portuguese (should be english).

Can't figure out how this plugin works =\

@marcglasberg
Copy link
Owner

marcglasberg commented Nov 5, 2019

Ok, no problem. Can you please provide me with some minimum runnable code (with main method and all) demonstrating the problem? I need to reproduce it to be able to fix it.

By the way, have you tried running the example? https://github.com/marcglasberg/i18n_extension/blob/master/example/lib/main.dart

@JCKodel
Copy link
Author

JCKodel commented Nov 5, 2019

Working as this:

On main.dart:

return MaterialApp(
      debugShowCheckedModeBanner: false,
      navigatorKey: Catcher.navigatorKey,
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [Locale("pt"), Locale("en"), Locale("es")],
      localeResolutionCallback: (locale, locales) {
        print("FALLBACK TO ${locale.toLanguageTag()}");

        if (locale.languageCode == "pt") {
          return const Locale("pt", "BR");
        } else if (locale.languageCode == "en") {
          return const Locale("en");
        } else if (locale.languageCode == "es") {
          return const Locale("es");
        }

        return const Locale("en");
      },

(needed to fallback to pt-BR when language is pt-*, otherwise, it won't work)

Then, with this translations:

  static var t = Translations("pt_br") +
      {
        "pt_br": "ENTRAR COM GOOGLE",
        "en": "ENTER WITH GOOGLE",
        "es": "INICIA COM GOOGLE",
      };

Now it works as intended (with my phone set to English United State, it calls the fallback code, so I set it to "en" and it works).

@marcglasberg
Copy link
Owner

You must wrap your widgets with I18n(child: ...) and you don't seem to be doing that:

@override
Widget build(BuildContext context) {
 return MaterialApp(
   home: I18n(child: ...)
 );
}

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

2 participants