Localizing game text
Load dictionaries and look up a string by key.
LocalesService gets translations from ConfigStore. Load json/locales/en.json and json/locales/ru.json before reading the first string. In the Russian dictionary, add "score": "Очки: {value}".
After loading, configure language selection. Here, locales is the service resolved with SystemServices.LocalesService from @gamedevland/engine/di:
locales.initialize({
supportedLocales: ['en', 'ru'],
defaultLocale: 'en',
deviceLocale: engine.platform.getPreferredLocale(),
storageKey: 'sdk-game:language',
});
locales.setLang('ru');
label.setText(locales.t('score', { value: 120 }));
initialize selects the saved language, then the platform hint, browser language or configured default. setLang saves a manual choice.
Use locales.subscribe(handler) to update labels and unsubscribe when the component is disabled. Changing the language does not rewrite Text nodes automatically. After setting a translation, label.fitTo(width, height) can shrink the font to fit the given bounds.
The catalog also supports es, pt-BR, de, fr, it, tr, zh-CN, ja and ko; the game selects its subset in supportedLocales. Each used language needs a prepared dictionary. If a key is missing from the current and fallback dictionaries, t returns the key.