How to use pluralization in ngx-translate without ngx-translate-messageformat-compiler plugin
Asked Answered
S

1

7

I had problems with ngx-translate-messageformat-compiler plugin (json files parse fails after add plural forms). ¿There are any alternatives?

Spaceband answered 6/9, 2019 at 9:9 Comment(0)
S
11

I resolved to implement a custom pipe:

Pipe

@Pipe({
  name: 'pluralTranslate',
  pure: false
})
export class PluralTranslatePipe implements PipeTransform {

  transform(key: string, number: number): string {

    return `${key}.${number == 0 ? 'none' : number == 1 ? 'singular' : 'plural'}`;
  }
}

Use

{{ 'daysNumber' | pluralTranslate:2 | translate:{ days: 2} }}

Messages

{
"daysNumber": {
      "none": "",
      "singular": "{{ days }} day",
      "plural": "{{ days }} days"
    },
}
Spaceband answered 6/9, 2019 at 9:9 Comment(1)
Note: This does not necessarily work for all lanuagesDetrain

© 2022 - 2024 — McMap. All rights reserved.