Translations
The words of the interface (the menu buttons, the settings, the dialogs) are interface strings: each one has a key, like Start or Save, and a text in each language. The engine comes with the strings in 18 languages and shows the ones of the player’s Language preference. This page lists the methods that hold the strings and put them on the screen. Translating Your Game shows how to translate a whole game, script included.
Strings
translation()
Reads the strings of one language, or adds some.
translation(language: string): object | undefined
translation(language: string, strings: object): objectWith strings, it merges them into the language’s own, and creates the language if it’s new. It returns the language’s strings either way.
monogatari.translation('English', {
'SkipChapter': 'Skip to the next chapter?',
});Add your own keys this way, to use them in an alert(), in the script with _(SkipChapter), or on an element with data-string="SkipChapter". Giving a key the engine has ('Start': 'Begin') changes the engine’s text.
The languages the engine has strings for, by the names the Language preference uses: Bahasa Indonesia, Български, Беларуская, Deutsch, English, Español, Français, Magyar, Nederlands, Português, Português do Brasil, Русский, اللغه العربية, 한국어, 日本語, 繁體中文, 简体中文 and toki pona.
translations()
Reads every language’s strings, or adds strings to several languages at once.
translations(): object
translations(language: string): object | undefined
translations(object: object): objecttranslations() returns every language, translations('English') one of them. With an object, the strings of each language it names are added to that language’s strings, as translation() does for one language, and a language it doesn’t have yet is added:
monogatari.translations({
'English': { 'SkipChapter': 'Skip this chapter' },
'Español': { 'SkipChapter': 'Saltar este capítulo' },
});Before 2.9.0, each language in the object replaced that language’s strings entirely.
string()
Returns the interface string of a key, in the player’s language.
string(key: string): string | undefinedmonogatari.string('Start'); // 'Start' in English, 'Comenzar' in EspañolWhen the player’s language has no string for the key, or no strings at all, it returns the English string, and undefined when English doesn’t have the key either. With the debug module, a missing key also opens an error dialog titled Translation for string "…" could not be found, and a missing language one titled Language could not be found, each once however many strings ask for it at a time.
translate()
Replaces every _(Key) in a text with its interface string.
translate(text: string): stringmonogatari.translate('_(Start) or _(Load)'); // 'Start or Load'A key with dots reaches into an object of strings: _(Chapters.One) reads { Chapters: { One: '…' } }. A key the player’s language doesn’t have is read from English, and a key no language has stays as it was. The key can’t hold spaces or parentheses, so markers next to each other or inside parentheses work: '(_(Start))' gives '(Start)'.
The engine runs every text statement through it (as part of replaceVariables()), so _(Key) works in dialog and in any statement of the script:
monogatari.script({
'Start': [
'y _(SkipChapter)',
'end',
],
});Translate the page
localize()
Writes the interface strings of the player’s language into the page.
localize(): voidIt fires willLocalize, then sets every element inside <visual-novel> that has a data-string attribute to the string of that key, and fires didLocalize. A key the language doesn’t have gets the English string; with the debug module it also opens an error dialog. An element whose key has no string in any language (or an empty one) keeps its text.
Four attributes are translated the same way, from an attribute that names the key: data-string-placeholder, data-string-title, data-string-aria-label and data-string-alt. The save screen’s name field uses the first:
<input type="text" data-string-placeholder="SaveSlotName">It also uses the language’s code (see below) for the language dates are written in, and for the page: <html lang="…"> gets the code, and <html dir="…"> gets rtl for a language written from right to left (Arabic, Persian, Hebrew, Urdu…) and ltr for the others. A language without a code leaves both as they were.
init() calls it, and so does the settings screen when the player picks another language. After you change the Language preference from code, call it yourself:
monogatari.preference('Language', 'Español');
monogatari.localize();The menus switch to Spanish: Save reads Guardar.
Give an element of your own a data-string attribute and it gets translated with the rest:
<p data-string="SkipChapter"></p>Describe a language
languageMetadata()
Reads or sets what the engine knows about a language beyond its strings.
languageMetadata(): object
languageMetadata(language: string): { code: string; icon: string } | undefined
languageMetadata(language: string, metadata: { code?: string; icon?: string }): { code: string; icon: string }| Key | What it is |
|---|---|
code |
The language’s code, such as 'es' or 'pt-br'. The save screens write dates in this language, the page’s lang attribute takes it, and it decides the writing direction: a code like 'ar', 'fa', 'he' or 'ur' turns the page right to left. |
icon |
What the language selection screen shows next to the language’s name: the engine uses flag emoji. |
monogatari.languageMetadata('English'); // { code: 'en', icon: '🇺🇸' }
monogatari.languageMetadata('Euskara', {
code: 'eu',
icon: '🏳️',
});Every language of a multi-language script needs metadata: on the language selection screen, a language without it gets no button, and the debug module opens an error dialog naming it.