Namespace locale

xeokit Localization Service


Message lookup and translation utilities.


This module provides a small localization layer for xeokit components. Translations are keyed by stable identifiers such as "NavCube.front" and resolved against the active locale.


  • Locale-keyed messagesloadMessages(locale, bundle) adds messages; setLocale(locale) switches the active locale.
  • Stable keys — UI widgets reference keys like "NavCube.front".
  • Argument interpolationtranslate("WELCOME", { name }) resolves {name} placeholders in the matched message.
  • PluralisationtranslatePlurals(key, count, args) selects the right plural form based on the active locale's plural rules.
  • Incremental load — bundles can be loaded per locale.
  • Event-driven updateonLocaleChanged / onMessagesLoaded notify subscribed widgets when locale data changes.

  • UI widgets that need dynamic language switching
  • Centralized management of translated strings
  • Incremental loading or replacement of translation bundles
npm install @xeokit/sdk

The example below shows how to create a LocaleService with English, Māori, and French translations for a NavCube widget.

The following keys are used by the NavCube:

  • "NavCube.front"
  • "NavCube.back"
  • "NavCube.top"
  • "NavCube.bottom"
  • "NavCube.left"
  • "NavCube.right"

These keys are resolved against the active locale. For example, when the locale is set to "fr", "NavCube.back" resolves to "Arrière".

import { LocaleService } from "@xeokit/sdk/base/locale";

const localeService = new LocaleService({
messages: {
en: { // English
NavCube: {
front: "Front",
back: "Back",
top: "Top",
bottom: "Bottom",
left: "Left",
right: "Right"
}
},
mi: { // Māori
NavCube: {
front: "Mua",
back: "Tuarā",
top: "Runga",
bottom: "Raro",
left: "Mauī",
right: "Tika"
}
},
fr: { // French
NavCube: {
front: "Avant",
back: "Arrière",
top: "Supérieur",
bottom: "Inférieur",
left: "Gauche",
right: "Droit"
}
}
},
locale: "en"
});

localeService.locale = "mi"; // Switch to Māori

New message bundles can be merged in at any time, without recreating the service:

localeService.loadMessages({
jp: { // Japanese
NavCube: {
front: "前部",
back: "裏",
top: "上",
bottom: "底",
left: "左",
right: "右"
}
}
});

localeService.clearMessages();

LocaleService emits an update event whenever the active locale changes or new messages are loaded. This allows UI components to re-render automatically.

localeService.onUpdated.subscribe(() => {
console.log(localeService.translate("NavCube.left"));
});

Classes

LocaleService