Translation
The functions that localise messages in scripts and components, and the Translation object that lists, generates, copies and deletes localised messages.
Servicely shows each message in the user’s language from LocalizedMessage records, one per message key and locale. This page lists the functions that look messages up in scripts and Angular components, and the Translation object that manages the records from a server script.
Get an instance
The lookup functions and Translation are globals in server scripts and script libraries. $translate and $translateKey also work in Angular component code.
let msg = $translate("<message key>")`Error approving: ${reason}`;
let missing = Translation.listMissingTranslations("fr_FR", Translation.ALL_APPLICATIONS);
Locales are written language, underscore, country, for example en_US or fr_FR.
Message lookup functions
A message can hold substitution points {0}, {1} and so on. In a template literal, each ${...} fills the next point. When there is no message for the user’s language, the default text in the template literal is used, and failing that, the key.
| Method | Returns | Description | Since |
|---|---|---|---|
$translate(key)`text ${value}` |
String | Looks up key for the user’s language and fills its substitution points with the template’s values. |
1.11 |
$translate`:@@key:text ${value}` |
String | The same, with the key written at the start of the template. | 1.11 |
$localize`:@@key:text ${value}` |
String | The same as the form above. | 1.11 |
$translateKey(key) |
String | The message for key with no substitution, or key itself when there is none. |
1.11 |
i18n.message(key) |
String | The message for key. i18n.getMessage(key) is the same. |
1.11 |
Messages in Angular components
In an Angular template, the i18n attribute replaces an element’s content with a localised message, and i18n-<attribute> replaces that attribute. The key in the template starts with @@, which the LocalizedMessage record leaves out: @@ng.<id>.PortalComponent.Template.demo.label in the template is ng.<id>.PortalComponent.Template.demo.label in the record.
<a i18n-href="@@ng.<id>.PortalComponent.Template.demo.href"
href="https://example.com">
<span i18n="@@ng.<id>.PortalComponent.Template.demo.label">
The default place is {{place}}
</span>
</a>
Always give default text. Without it, no substitution is made. An Angular expression such as {{place}} in the default text is a substitution point, so the record’s message reads The default place is {0}. A translation can move {0} to wherever the language needs it. Named, number and plural substitution points are not supported.
Keys take the form ng.<id>.<TableName>.<FieldName>.<suffix>, with no spaces in the suffix. In a Servicely code editor, generate one from the context menu or with Ctrl+F12 (Cmd+F12 on macOS).
Translation object
Translation manages LocalizedMessage records and calls a translation service to generate them. Set up the service first, as described in Set up dynamic translation. Where a method has no fromLocale, it uses en_US. Where it takes an application, pass its name or ID, or "*" for every application.
| Method | Returns | Description | Since |
|---|---|---|---|
Translation.EN_US |
String | "en_US". |
1.11 |
Translation.ALL_APPLICATIONS |
String | "*". |
1.11 |
translate(toLocale, text) |
String | Translates text with the translation service. It creates no record, so use it to test the service. |
1.11 |
translate(fromLocale, toLocale, text) |
String | The same from another locale. text can be an array, and then an array is returned. |
1.11 |
listTranslations(app) |
Array | The en_US messages of an application. |
1.11 |
listTranslations(fromLocale, app) |
Array | The messages of an application in fromLocale. |
1.11 |
listMissingTranslations(toLocale, app) |
Array | The en_US messages of an application that have no message in toLocale. |
1.11 |
listMissingTranslations(fromLocale, toLocale, app) |
Array | The same from another locale. | 1.11 |
generateMissingTranslations(toLocale, fromApp, toApp) |
Array | Translates every missing message of fromApp and saves it in toApp, which should be the locale’s language pack. |
1.11 |
generateMissingTranslations(fromLocale, toLocale, fromApp, toApp) |
Array | The same from another locale. | 1.11 |
regenerateTranslations(toLocale, fromApp, toApp) |
Array | Translates every message of fromApp again, including ones that exist, and saves them in toApp. |
1.11 |
regenerateTranslations(fromLocale, toLocale, fromApp, toApp) |
Array | The same from another locale. | 1.11 |
regenerateSpecificTranslations(toLocale, fromApp, toApp, keys) |
Array | Translates again only the messages whose keys are in the array keys. |
1.11 |
regenerateSpecificTranslations(fromLocale, toLocale, fromApp, toApp, keys) |
Array | The same from another locale. | 1.11 |
generateLocalizedMessage(toLocale, fromApp, toApp, key) |
LocalizedMessage | Translates one message and saves it in toApp. |
1.11 |
generateLocalizedMessage(fromLocale, toLocale, fromApp, toApp, key) |
LocalizedMessage | The same from another locale. key can be an array, and then an array is returned. |
1.11 |
replaceLocalizedMessage(locale, app, text, replacement) |
Array | Replaces every message whose whole text is text with replacement. |
1.11 |
patchLocalizedMessage(locale, app, text, replacement) |
Array | Replaces text with replacement wherever it appears inside a message. |
1.11 |
copyLocalizedMessage(toLocale, fromApp, toApp, key) |
LocalizedMessage | Copies the en_US message to toLocale unchanged, so the missing-message check stops listing it. key can be an array. |
1.11 |
copyLocalizedMessage(fromLocale, toLocale, fromApp, toApp, key) |
LocalizedMessage | The same from another locale. key can be an array. |
1.11 |
deleteTranslation(locale) |
Array | Deletes every message in locale. An English locale cannot be deleted whole and raises an error. |
1.11 |
deleteTranslation(locale, app) |
Array | Deletes the messages of one application in locale. |
1.11 |
deleteTranslation(locale, app, key) |
Array | Deletes one message, or several when key is an array. |
1.11 |
ensureTranslationExists(locale, key, text) |
LocalizedMessage | Returns the message for key in locale, creating it with text when there is none. |
1.11 |
ensureBaseTranslationExists(key, text) |
LocalizedMessage | The same for en_US. |
1.11 |
prepareLanguagePackUpgrade(locale) |
Nothing | Installs the language pack application translations-<locale> if it is missing, and every application not yet installed, with its demo data. |
1.11 |
performLanguagePackUpgrade(locale, createChangeSet) |
String | Generates the missing messages of every installed application into the language pack, and returns the pack’s name. With createChangeSet true, it first creates and opens a change set for the pack. |
1.11 |
performLanguagePackRegeneration(locale, createChangeSet) |
String | The same, but translates every message again. | 1.11 |
Examples
Test the translation service
Servicely Script
Translation.translate("fr_FR", "I am an elk");
Response
"je suis un élan"
Generate the missing messages for a language pack
let missing = Translation.listMissingTranslations("th_TH", "*");
Translation.generateMissingTranslations("th_TH", "*", "translations-th_th");
Fix a translated message
// Replace a message whose whole text is "NON"
Translation.replaceLocalizedMessage("fr_FR", "translations-fr_fr", "NON", "Non");
// Keep the en_US table name in Spanish
Translation.copyLocalizedMessage("es_ES", "itsm", "translations-es_es", "table.incident.name");