question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with 'few' value using NgPlural

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/../i18n/localization

Description

I am using the directive [ngPlural] my code is as follows

<label [ngPlural]="viewFlight.nights">
   <ng-template ngPluralCase="=1" i18n="@@predictor_itinerary_night">noche</ng-template>
   <ng-template ngPluralCase="few" i18n="@@predictor_itinerary_nights">noches</ng-template>
</label>

According to the documentation (https://angular.io/api/common/NgPlural) The code should work

Also in angular.json> build> options> I have set the default language of the application as: "i18nLocale": "es" I don’t know if this is important since I can’t think of anything else.,

🔥 Exception or Error

Error: No plural message found for value "3"
// The error appears with any value greater than 1



🌍 Your Environment

Angular Version:

9.0.5



Anything else relevant?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
petebacondarwincommented, May 30, 2020

@rockument69 - I think adding these links would be helpful, but we should make it clear that most locales do not support many of the options. We should also point out that other should always be implemented since that is the standard fallback for a missing option.

The detail is that the case is computed by calling the getPluralCase() function:

const pluralMapping = ['zero', 'one', 'two', 'few', 'many'];

/**
 * Returns the plural case based on the locale
 */
export function getPluralCase(value: string, locale: string): string {
  const plural = getLocalePluralCase(locale)(parseInt(value, 10));
  const result = pluralMapping[plural];
  return (result !== undefined) ? result : 'other';
}

You can see that this will return one of ['zero', 'one', 'two', 'few', 'many'] or 'other' based on a call to the function returned from getLocalePluralCase(). This function is defined in the locale data file of the given locale. For example, in the ar-AE locale file:

function plural(n: number): number {
  if (n === 0) return 0;
  if (n === 1) return 1;
  if (n === 2) return 2;
  if (n % 100 === Math.floor(n % 100) && n % 100 >= 3 && n % 100 <= 10) return 3;
  if (n % 100 === Math.floor(n % 100) && n % 100 >= 11 && n % 100 <= 99) return 4;
  return 5;
}

Note that the numbers returned correspond to the index into the ['zero', 'one', 'two', 'few', 'many'] array.

But also note that the vast majority of locales implement a much more simple plural() function such as:

function plural(n: number): number {
  let i = Math.floor(Math.abs(n)), v = n.toString().replace(/^[^.]*\.?/, '').length;
  if (i === 1 && v === 0) return 1;
  return 5;
}

See that in this case the function will only ever return 1 ('one') or 5 ('other').

0reactions
angular-automatic-lock-bot[bot]commented, Jul 23, 2020

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't get ngPlural to work Angular - Stack Overflow
When I use [ngPlural] I get the error No provider TemplateRef! for Template ref. If I remove it and use *ngPlural I also...
Read more >
ngPlural & ngPluralCase : Pluralization in Angular
Converting singular words to plural is often difficult Even in english where most ... Error: No plural message found for value - ngPlural....
Read more >
NgPlural - Angular
Displays DOM sub-trees that match the switch expression value, or failing that, DOM sub-trees that match the switch expression's pluralization category. To use...
Read more >
Angular 10 NgPlural Directive - GeeksforGeeks
The NgPlural in Angular10 is used to Add or remove DOM sub-trees based on a numeric value. Syntax: <li *NgPlural='condition'></li>. NgModule: ...
Read more >
NgPlural - Angular
To use this directive you must provide a container element that sets the ... <some-element [ngPlural]="value"> <ng-template ngPluralCase="=0">there is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found