-
-
Notifications
You must be signed in to change notification settings - Fork 336
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
Fix #648: allow using "old" (e.g. pre-3.0.4) plural rules evaluation #668
Conversation
…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.
Why would we allow modifying a language's plural behavior? What is the use case? |
This is to restore the old behavior that made sense in a lot of use cases, where "zero" and "one" have better (more meaningful) variations than "other". For example:
Here, "nobody" is better than "0 members" but without this fix I can't have it in English. Another example:
Here I have special versions of "zero" and "one" that would sound better for the user. |
Thanks for the explanation! I understand why that's helpful for your situation. However, it feels wrong to do so because it missuses plurals. Imho the correct solution would be:
Open for opinions, what do the others think? |
But the usage of this would be final string = qty == 0 ? LocaleKeys.label_members_none.tr() : LocaleKeys.label_members_none.plural(qty); which is a bit unreadable and it gets even worst with the second scenario: final string = qty == 0 ? LocaleKeys.label_members_none.tr() :
qty == 1 ? LocaleKeys.label_members_one.tr() :
LocaleKeys.label_members_none.plural(qty); Since the behavior is behind an optional flag I think it's good to let developers choose: if anyone disagrees they just have to do nothing, because out-of-the-box the behavior is the "correct" one. Also notice that for the end user it's totally transparent: users will just see the desired text and they don't care how it's built under the hood. |
Well, what is the scope of easy_localization? Your goal is to show two entirely different texts based on the count of a variable. I would not want to make this a feature of easy_localization. |
Well, the original issue has 5 thumb-ups so I'm clearly not the only one that feels this is a regression... |
Furthermore, the official flutter packages behave in the same way, for example if I use {
"pushed": "You have pushed the button this many times:",
"nTimes": "{count, plural, =0{never} =1{once} other{{count} times}}"
} Text(AppLocalizations.of(context)!.nTimes(_counter)), Here's the output: So, the previous behavior was legit. Full example: https://github.com/easyhour/easy_localization/tree/i18n_demo Official Flutter docs: https://docs.flutter.dev/ui/accessibility-and-internationalization/internationalization |
Thanks for elaborating on this. The argument that the official flutter translations implementation has this behavior is very convincing. This would make the behavior more clear imho. Wdyt @mauriziopinotti ? |
Sure, thanks, I'll submit an updated patch in the next days! |
Parameter forcePluralCaseFallback has been renamed to ignorePluralRules for clarity as suggested by bw-flagship. Also, the logic is now reversed (e.g. false will implement the old behavior and true will implement the new behavior). Finally, the default value is now false (old behavior).
Hi @bw-flagship , I've pushed a new commit with the renamed parameter. While working on this I had two doubts
So, to clarify, now the patch works like this:
Let me know if this is OK or more changes are needed. Thanks! |
lib/src/localization.dart
Outdated
@@ -114,6 +119,9 @@ class Localization { | |||
} | |||
|
|||
static PluralRule? _pluralRule(String? locale, num howMany) { | |||
if (!instance._ignorePluralRules) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instance._ignorePluralRules
@mauriziopinotti I would reverse it: ignorePluralRules is for me the same as ForceUsingFallback ignorePluralRules = true should cause the behavior if 3.0.4 and earlier (and can be default) |
ignorePluralRules=true, "old" beahvior (default) ignorePluralRules=false, "new" beahvior
OK, it should be good now. |
the logic looks good! Found some formating things and you need to integrate the latest dev changes, thanks! |
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.