The i18nPlural pipe

The i18nPlural pipe has a simple usage, where we just evaluate a numeric value against an object mapping different string values to be returned depending on the result of the evaluation. This way, we can render different strings on our template depending if the numeric value is zero, one, two, more than N, and so on. The syntax is the following:

expression | i18nPlural:mapping[:locale]

Let's look at how this could look for a numeric field jedis on your component class:

<h1> {{ jedis | i18nPlural:jediWarningMapping }} </h1>

Then, we can have this mapping as a field of our component controller class:

export class i18DemoComponent {
jedis: number = 11;
jediWarningMapping: any = {
'=0': 'No jedis',
'=1' : 'One jedi present',
'other' : '# jedis in sight'
}
}

We even bind the numeric value evaluated in the expression by introducing the '#' placeholder in the string mappings. When no matching value is found, the pipe will fall back to the mapping set with the key 'other'.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset