Unity SDK

Localization

Pull every translation key for your project, auto-detect the player's language, and resolve strings with graceful fallback. Ships with a disk cache so the first frame after a cold launch is fully translated even offline.

Overview

KalmForge.Localization is a static class. Call Load() once after KalmForgeClient.Init() and from then on every Get(key) resolves against the active language with fallback to your project's default language, then to the key itself.

  • ~40 ISO codes supported out of the box (full list in the dashboard).
  • System language is auto-detected on first launch - saved to PlayerPrefs after the first SetLanguage.
  • The full payload is cached on disk at Application.persistentDataPath/kalmforge_localization.json.

Quick start

LocalizedHud.csC#
1using KalmForge;
2using UnityEngine;
3using UnityEngine.UI;
4
5public class LocalizedHud : MonoBehaviour {
6 [SerializeField] Text title;
7 [SerializeField] Text body;
8
9 async void Start() {
10 await KalmForgeClient.Init();
11 await Localization.Load();
12
13 Refresh();
14 Localization.OnLanguageChanged += Refresh;
15 }
16
17 void Refresh() {
18 title.text = Localization.Get("hud.title");
19 body.text = Localization.Format("hud.coins", playerCoins);
20 }
21}

Languages & detection

After Load() resolves, AvailableLanguages contains every code your project ships. The active language is picked in this order:

  1. Whatever the player previously chose (persisted in PlayerPrefs under kalmforge.language).
  2. The exact match for Application.systemLanguage (e.g. pt-br).
  3. The base language code (pt when the system says pt-br).
  4. The project's default language.
exampleC#
1// Show a language picker
2foreach (var code in Localization.AvailableLanguages) {
3 Debug.Log(code); // "en", "fr", "pt-br", ...
4}
5
6// Switch - returns false if the code is unknown.
7bool ok = Localization.SetLanguage("fr");
Note
Codes are normalised: leading/trailing whitespace stripped, lower-cased. "PT-BR" and "pt-br" are the same key.

Resolving keys

Get(key) walks current language → default language → first non-empty value → the key string itself. It never returns null - a missing key shows up as the literal key, which is your bug-finding fallback.

exampleC#
1string label = Localization.Get("store.gem_pack.title");
2string formatted = Localization.Format("store.gem_pack.price", 4.99f);
3bool defined = Localization.Has("store.gem_pack.title");
Heads up
Format uses CultureInfo.InvariantCulture. If your translators include locale-specific number formatting in the template (e.g. {0:N2}), validate the output for every language.

Reacting to language changes

OnLanguageChanged fires whenever the dataset is loaded or SetLanguage succeeds. Re-bind your UI from this single handler:

exampleC#
1void OnEnable() { Localization.OnLanguageChanged += Rebind; }
2void OnDisable() { Localization.OnLanguageChanged -= Rebind; }
3void Rebind() { /* refresh every label */ }

Cache & offline

Load() tries the network first; on failure it falls back to kalmforge_localization.json in persistentDataPath. A successful network response overwrites the cache. The cache file name is exposed as Localization.CacheFileName - delete it to force a clean re-download.

API reference

Localization - static API
NameTypeDescription
AvailableLanguagesIReadOnlyList<string>All language codes shipped by your project.
DefaultLanguagestringProject default. Used as final fallback for Get().
CurrentLanguagestringActive language code.
IsLoadedboolTrue after a successful Load().
OnLanguageChangedevent ActionFired on Load() or SetLanguage().
Load()TaskFetch translations (network → cache fallback).
SetLanguage(code)boolSwitch active language. Returns false if code unknown.
Get(key)stringResolve key (current → default → first non-empty → key literal).
Format(key, args)stringGet() then string.Format with InvariantCulture.
Has(key)boolTrue if a value exists in current OR default language.
CacheFileNamestring (const)"kalmforge_localization.json".

REST endpoint

examplebash
1GET /api/public/sdk/localization
2Headers:
3 X-API-Key: kf_xxx_yyy
4
5Response:
6{
7 "default_language": "en",
8 "languages": ["en", "fr", "pt-br", ...],
9 "keys": [
10 { "key": "hud.title", "values": { "en": "Score", "fr": "Score" } },
11 ...
12 ]
13}
Back to DocsKalmForge SDK · v1.0.1