I am trying to "translate" my angular application using angular-translate
.
But I am stuck with paramaterized translation in a plural
.
index.html includes:
<script src="assets/libs/angular-1.5.7/angular.js"></script>
<script src="assets/libs/angular-1.5.7/angular-message-format.js"></script>
<script src="assets/libs/message-format-1.0.0-rc3/messageformat.js"></script>
<script src="assets/libs/angular-translate-2.11.1/angular-translate.js"></script>
<script src="assets/libs/angular-translate-2.11.1/angular-translate-loader-static-files.js"></script>
<script src="assets/libs/angular-translate-2.11.1/angular-translate-interpolation-messageformat.js"></script>
Then, I configure angular-translate:
angular.module('app').config(['$translateProvider', function ($translateProvider) {
// Language files folder definition
$translateProvider.useStaticFilesLoader({
prefix: 'app/resources/locale-',
suffix: '.json'
});
// Default language
$translateProvider.preferredLanguage('fr_FR');
// TODO does logging missing key to console should be activated in production?
$translateProvider.useMissingTranslationHandlerLog();
//$translateProvider.useMessageFormatInterpolation();
// Force not using sanitizer (to avoid security warning)
$translateProvider.useSanitizeValueStrategy(null);
}])
Then, i define some translation keys in a static file:
{
"drivers.results.noresult" : "No result",
"drivers.results.oneresult" : "One result",
"drivers.results.nresults" : "{{length}} results"
}
And I try to translate a message with the plural syntax:
{{ nbResults, plural,
=0 { {{'drivers.results.noresult' | translate}} }
=1 { {{'drivers.results.oneresult' | translate}} }
other { {{'drivers.results.nresults' | translate:'{length: nbResults }' }} }
}}
When nbResults=0
or 1
, everything is ok, I got the correct message.
But, in other case, example if nbResults=10
, the length is not injected into the message key. And the message displayed is only "results" instead of "10 results".
Note: if I change the last line to
other { {{'drivers.results.nresults' | translate:'{length: 10 }'
I've got the message 10 results
.
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.
The service also contains a method called translate. It is later called by the pipe to get the translation for a specific key. It always uses the language specified in the 'language'-variable of the service, to lookup the translation. That's it.
It lets you define translations for your content in different languages and switch between them easily. Check out the demo on StackBlitz. It gives you access to a service, a directive and a pipe to handle any dynamic or static content. NGX-Translate is also extremely modular.
I finally find the solution of this problem.
So the language key is still:
"drivers.results.nresults" : "{{length}} results"
This is working fine with a static value: (note the simple quotes)
{{ nbResults, plural,
=0 { {{'drivers.results.noresult' | translate}} }
=1 { {{'drivers.results.oneresult' | translate}} }
other { {{'drivers.results.nresults' | translate:'{length: 123 }' }} }
}}
But if I want something dynamic, I need to write this (without quote):
{{ nbResults, plural,
=0 { {{'drivers.results.noresult' | translate}} }
=1 { {{'drivers.results.oneresult' | translate}} }
other { {{'drivers.results.nresults' | translate:{length: nbResults } }} }
}}
And it will replace correctly the length parameter in the translation!
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