I had problems with ngx-translate-messageformat-compiler plugin (json files parse fails after add plural forms). ¿There are any alternatives?
How to use pluralization in ngx-translate without ngx-translate-messageformat-compiler plugin
Asked Answered
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"
},
}
Note: This does not necessarily work for all lanuages –
Detrain
© 2022 - 2024 — McMap. All rights reserved.