Namespace locale

xeokit Localization Service


Provides locale-specific translations for words and phrases.



npm install @xeokit/sdk

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

The LocaleService provides translations for:

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

These terms act as keys that map to translations based on the active locale. For example, if the locale is set to "fr", "NavCube.back" resolves to "Arrière".

import { LocaleService } from "@xeokit/sdk/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

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

localeService.clearMessages();

The LocaleService emits an event when the locale changes or new messages are loaded. This can be useful for refreshing UI elements dynamically.

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

Classes

LocaleService