Goals: Allowing multiple languages to be incorporated to the impac dashboard project (currently exclusively english).
...
All the translations are kept in data-interchange formats (json, yaml, ...) that are then compiled as an angularjs module.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
{ "my.awesome.translation.of.hello_world": "Hello World", "namespace.to.avoid.confusion.or.conflicts": "Each variable has a unique name to avoid conflicts" } |
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
$translate('my.value.to.translate.hello_x', {name: 'John'}).then( (translation) -> scope.yourVariable = translation ) |
Pluralization
You might need to add pluralization to your translations. In order to do that we use an additional module called messageformat.
It is enabled by default so you don't need to add anything.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<p translate="mno_enterprise.templates.impac.dock.settings.nb_of_users"
translate-values="{ num_users: app.licences_count }"></p> |
The translation would have the following schema:
{name_of_your_variable, typeOfFormat (plural or gender), =case_1{translation 1} =case_2{translation 2} other{translation by default}}
E.g:
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
{
"impac.name.path.nb_of_users": 'There {num_users, plural, =0{are no users} =1{is one user} other{are # users}}';
} |
Any question or more examples, details: look at the well made documentation
...
NEVER hardcode text in a template or in a directive: use a key that has a translation in the json file
Code Block language xml theme RDark title src/views/**/some-views.html linenumbers true <!-- Wrong --> <p>This is wrong</p> <!-- Correct --> <p translate>impac.name.path.this_is_better</p>
Code Block language xml theme RDark title src/locales/en-gb.json linenumbers true { "impac.name.path.this_is_better": "This is better" }
If you need to get text from the back-end, add a key field to the response and process it in the front-end.
Code Block language js theme RDark linenumbers true title: "I am an english title" //Add this field title_key: "impac.name.path.title"
Always create a new key, even if there is already a translation for it.
If there is already a translation, you may assign it like a variable.This allow more flexibility to change the name if needed.
Code Block language xml theme RDark title src/locales/en-gb.json linenumbers true { "impac.common.action.cancel": "Cancel, "impac.widget.accounting_values.turnover.cancel": "@:impac.common.action.cancel" }
Remember that translations take into account gender and pluralization.
...