aissat / easy_localization

Easy and Fast internationalizing your Flutter Apps
https://pub.dev/packages/easy_localization
MIT License
928 stars 335 forks source link

Plurals does not support zero e.g. for en #696

Open rekire opened 5 months ago

rekire commented 5 months ago

I am aware that there is no official zero case for the English language and that the default other is technically correct, but supporting the zero case would be nice to handle empty states.

As sample you can use the money example from the readme:

{
  "money": {
    "zero": "You not have money",
    "one": "You have {} dollar",
    "many": "You have {} dollars",
    "other": "You have {} dollars"
  }
}

When I use 'money'.plurals(0, args: [0]) I get the unexpected output: 'You have 0 dollars'. Expected would be 'You not have money'.

petodavid commented 4 months ago

Did you set ignorePluralRules to false like this? By default the rules are ignored

    return EasyLocalization(
      supportedLocales: supportedLocales,
      fallbackLocale: supportedLocales.first,
      ignorePluralRules: false,
      path: 'lib/localization',
    );
rekire commented 4 months ago

Interesting... The documentation for that field is this:

  /// Ignore usage of plural strings for languages that do not use plural rules.
  /// @Default value false
  /// Example:
  /// ```
  /// // Default behavior, use "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.
  /// // "nTimes": "{count, plural, =0{never} =1{once} other{{count} times}}"
  /// // Text(AppLocalizations.of(context)!.nTimes(_counter)),
  /// // will print "never, once, 2 times" for ALL languages.
  /// ignorePluralRules: true
  /// // Use "zero" rule for 0 only if the language is set to do so (e.g. for
  /// "lt" but not for "en").
  /// // "nTimes": "{count, plural, =0{never} =1{once} other{{count} times}}"
  /// // Text(AppLocalizations.of(context)!.nTimes(_counter)),
  /// // will print "never, once, 2 times" ONLY for languages with plural rules.
  /// ignorePluralRules: false
  /// ```
  final bool ignorePluralRules;

But the constructor is this:

  EasyLocalization({
//...
    this.ignorePluralRules = true,
//...
  })

That does not make sense. I'll check that later again.

By the way @petodavid do you know if this flag breaks languages with multiple plural forms?

petodavid commented 4 months ago

You should expect no braking change in case you handle the rule cases (ZERO, ONE, TWO, FEW, MANY, OTHER), this depends on the language and not all languages are using all theses cases. For example in English we have ONE and OTHER. Here you can find all the rules: Rules.

Rule for English is this:

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