Localizations are short text-snippets identified by a unique key, which have multiple translations into different languages. We can manage Localizations through the Localizations Service. In the context of Templates, we can use Localizations to create localize-able templates. In templates, we represent localizations with the helper function t. Take the following Template for example, where we use a Localization with key greeting:
In the original Template example, the greeting “Dear” was hardcoded. We have now replaced it with a greeting Localization.
This allows the Template Service to resolve the appropriate translated greeting dynamically. For example, if we resolve the Template in Dutch (NL), using the following snippet:
Eventually, the resolver will put everything together correctly and respond with the following resolved Template:
Or, if we asked the resolver to resolve the Template in English:
The response would be:
Localizations with arguments
Because of grammar rules in certain languages, the order of words in a sentence can differ. Of course, this poses a problem if we use Localizations like in the example mentioned above. Sometimes a Localization will need to determine the exact placement of variable content in a string. Therefore Localizations can accept arguments.
Arguments can be used in Localization by using the placeholders {{place_holder_name}} .
With a Template:
Resolving for English:
will yield the following response:
Or resolving for Dutch (NL), will yield the following response: (with a different order of the values)
{
"key": "heart_rate_msg",
"text": {
"EN": "On {{date}} at {{time}}, your heart rate was {{rate}} bpm.",
"ES": "El {{date}} a la(s) {{time}}, su frecuencia cardíaca era de {{rate}} lpm.",
"NL": "Uw hartslag was {{rate}} spm op {{date}} om {{time}}."
}
}
{
"name": "myTemplateName",
"inputs": {
"date": { "type": "string" },
"time": { "type": "string" },
"rate": { "type": "number" }
},
"outputs": {
// This example is just supplying the Localization with its arguments
"message": "{{t 'heart_rate_msg' [email protected][email protected][email protected] }}"
}
}