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

Using plural(0) count as other #648

Closed
DafiMjd opened this issue Feb 15, 2024 · 6 comments
Closed

Using plural(0) count as other #648

DafiMjd opened this issue Feb 15, 2024 · 6 comments

Comments

@DafiMjd
Copy link

DafiMjd commented Feb 15, 2024

I have this json:
"test": {
"zero": "0Test",
"one": "1Test",
"many": "many{digit} Tests",
"other": "other{digit} Tests"
}

when i use LocaleKeys.test.plural(0) it outputs as other

@mauriziopinotti
Copy link
Collaborator

Same here, it's a regression in version 3.0.4 (3.0.3 works correctly).

@mauriziopinotti
Copy link
Collaborator

@aissat any update on this issue? Looks like you made a new release but this fix is not included... it's a major regression so I think it should have priority over new feature, right?

@flavio-silva
Copy link

Same here, I have to downgrade it to 3.0.3

@dimzeta
Copy link

dimzeta commented Apr 11, 2024

Same thing, still present in 3.0.5.

But this is not a bug, this is a feature 🤓

This is present since this PR: #620, which now takes into account pluralization according to language.

final pluralRule = _pluralRule(_locale.languageCode, value);
final pluralCase = pluralRule != null ? pluralRule() : _pluralCaseFallback(value);

Pluralization is now based on the rules in plural_rules.dart. These rules are written from The Unicode CLDR (Common Locale Data Repository), and only a few languages have a specific rule for zero.

If your selected language does not have a specific rule for zero, it will use the "one" or "other" translation. Example for English:

PluralCase _en_rule() {
  if (_i == 0 || _i == 1) {
    return ONE;
  }
  return OTHER;
}

I used to do something like this, which does not work anymore:

{
    "zero": "No card",
    "one": "1 card",
    "other": "{} cards"
}

To fix this, we have to use it like this:

{
    "zero": "No card",
    "one": "{} card",
    "other": "{} cards"
}

As you can see, the bad news here is we can't no longer use "zero", unless the language has a specific rule.

To me, it will be nice to include "zero" to every language, to be able to display a custom translation in this case.

mauriziopinotti added a commit to easyhour/easy_localization that referenced this issue Apr 28, 2024
…ation

Add forcePluralCaseFallback option to force evaluation of fallback
plural rules, i.e.

* forcePluralCaseFallback: false
Default behavior, will use "zero" rule for 0 only if the language
is set to do so (e.g. for "lt" but not for "en").

* forcePluralCaseFallback: true
Force using "zero" rule for 0 even if the language doesn't use it
by default (e.g. "en"). If "zero" localization for that string
doesn't exist, "other" is still used as fallback.
@mauriziopinotti
Copy link
Collaborator

I have sent PR #668 for this: there's a new forcePluralCaseFallback option that can be set to true to enable the "legacy" behavior (pre-3.0.4). It defaults to false though, so that the new behavior is still used by default.

  runApp(EasyLocalization(
    // ...
    forcePluralCaseFallback: true,
    child: ...
  ));

@mauriziopinotti
Copy link
Collaborator

mauriziopinotti commented May 13, 2024

Commenting for clarity: after upgrading to easy_localization 3.0.7 you'll get the "old" behavior by default, with an option (ignorePluralRules) to switch to the new one.

So, if you have

  "label_members": {
    "zero": "nobody",
    "one": "{} member",
    "two": "{} members",
    "other": "{} members"
  },

then

LocaleKeys.label_members.plural(0)

will give

  • on easy_localization up to 3.0.6: nobody
  • on easy_localization 3.0.6: 0 members
  • on easy_localization 3.0.7: nobody
  • on easy_localization 3.0.7 with ignorePluralRules: false: 0 members

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

4 participants