Hello I need to make translations with pluralization depending on a value, but can't find how to do that.
for example I have variable peopleCount
.
peopleCount = 1
translations should be: english: {{ peopleCount }} person likes this
lithuanian: {{ peopleCount }} zmogus tai megsta
peopleCount
is more than 1 english translation would be: {{ peopleCount }} people like this
.but for lithuanian translations:
peopleCount
is between 2 and 9 or any number ending with those numbers except numbers which ends with numbers provided in 4th rule (example: 225, 249, 210 <--- these are correct numbers. and incorrent ones: 215, 250...). it would be {{ peopleCount }} zmones tai megsta
{{ peopleCount }} zmoniu tai megsta
How do I accomplish that?
AngularJS supports inbuilt internationalization for three types of filters currency, date and numbers. We only need to incorporate corresponding js according to locale of the country. By default it handles the locale of the browser.
What? angular-translate is an AngularJS module that makes your life much easier when it comes to i18n and l10n including lazy loading and pluralization.
AngularJS supports i18n/l10n for date, number and currency filters. Localizable pluralization is supported via the ngPluralize directive. Additionally, you can use MessageFormat extensions to $interpolate for localizable pluralization and gender support in all interpolations via the ngMessageFormat module.
Something like this would work for your scenario:
<ng-pluralize count="peopleCount"
when="{
'one': 'zmogus tai megsta',
'few': '{} zmones tai megsta',
'other': '{} zmoniu tai megsta'}">
</ng-pluralize>
You can look into this for more details. https://docs.angularjs.org/api/ng/directive/ngPluralize and for language specific plural strings here: http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
Angular-translate has service with functionality of MessageFormat which is really powerful and also has built-in locale for lithuanian. Article about MessageFormat and angular-translate.
Installing
You can install this package via bower:
$ bower install angular-translate-interpolation-messageformat
After that include necessary scripts with MessageFormat and angular-translate-interpolation-messageformat in that order:
<script src="path/to/messageformat.js"></script>
<script src="path/to/angular-translate-interpolation-messageformat.js"></script>
And finally in your config function call useMessageFormatInterpolation function from $translateProvider:
$translateProvider.useMessageFormatInterpolation();
Usage
After installing angular-translate-interpolation-messageformat
into your app you can work with it.
For example, you can create english localization for code 'PEOPLE' as this:
{
"PEOPLE" : "{peopleCount, plural, one {There is one man (in lithuanian)} few {# zmones tai megsta} other {# zmoniu tai megsta}}"
}
And than use it in your html like this:
<span translate="PEOPLE" translate-values="{peopleCount: 12}" translate-interpolation="messageformat"></span>
The output will be: "12 zmones tai megsta".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With