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

Complete plurals support. #44

Closed
shushper opened this issue Feb 14, 2020 · 0 comments
Closed

Complete plurals support. #44

shushper opened this issue Feb 14, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@shushper
Copy link

For now method AppLocalizations.plural supports only .zero .one and .other. It is good for the English language. But, for example, Russian needs support of .two .few and .many.

{
  "day": {
    "zero":"{} дней",
    "one": "{} день",
    "two": "{} дня",
    "few": "{} дня",
    "many": "{} дней",
    "other": "{} дней"
  }
}

It will be perfect to delegate plurals logic to a reliable library. I chose the Intl library and it works great for me. Intl package already included in dart so there is no need for external dependencies. I replaced plural method with the following code:

String plural(String key, dynamic value) {
   final res = Intl.pluralLogic(
     value,
     zero: this._resolve(key + '.zero', this._localizedStrings),
     one: this._resolve(key + '.one', this._localizedStrings),
     two: this._resolve(key + '.two', this._localizedStrings),
     few: this._resolve(key + '.few', this._localizedStrings),
     many: this._resolve(key + '.many', this._localizedStrings),
     other: this._resolve(key + '.other', this._localizedStrings),
     locale: localeName
   );

   return res.replaceFirst(RegExp(r'{}'), '$value');
}

Method _resolve should return null if it does not find a res by key. Be cause different languages may not use some plurals.

@aissat aissat added the enhancement New feature or request label Feb 15, 2020
@aissat aissat closed this as completed Feb 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants