Localizing game text
Load dictionaries and look up a string by key.
LocalesService gets translations from ConfigStore. LocaleCatalog supports en and ru, expecting the IDs json/locales/en.json and json/locales/ru.json respectively.
This localizes the game itself, rather than the course website. The public Block Puzzle project leaves some labels directly in the layout. The following example shows how to connect dictionaries in a new project.
Include both JSON files in the manifest as json and load them before accessing LocalesService. Add button and counter labels to the Russian dictionary:
{
"restart": "Начать заново",
"score": "Очки: {value}"
}
In the component, select a language and substitute the score into the translation. Here, label is a Text node, and SystemServices is imported from @gamedevland/engine/di:
const locales = this.node.engine.di.get(SystemServices.LocalesService);
locales.setLang('ru');
label.setText(locales.t('score', { value: 120 }));
setLang changes the current language but does not automatically rewrite existing Text nodes. The component must set the strings again. getLang() returns the current language, and has(key) checks whether a translation exists.
When looking up a string, the service checks the current dictionary and fallback. If the key is missing, it returns the key itself. A missing dictionary is different: accessing an unprepared file does not automatically load it successfully.
At startup, the engine can choose a language using the platform’s hint. Dictionaries for the lookup order must be prepared in advance. Adding a language requires updating the SDK’s catalog; a de.json file alone is not enough.