UNPKG

@intuitionrobotics/thunderstorm

Version:
42 lines 1.66 kB
import {} from "./types.js"; import { ImplementationMissingException, Module } from "@intuitionrobotics/ts-common"; import { format } from "util"; import { ThunderDispatcher } from "../../core/thunder-dispatcher.js"; import { StorageKey } from "../StorageModule.js"; const dispatch_onLanguageChanged = new ThunderDispatcher("__onLanguageChanged"); export class LocaleModule_Class extends Module { constructor() { super("LocaleModule"); } activeLocale; defaultLocale; selectedLanguage = new StorageKey("locale--selected-language"); init() { const defaultLocale = this.selectedLanguage.get() || this.config.defaultLocale; if (!defaultLocale) throw new ImplementationMissingException("MUST set defaultLocale in the config data"); this.defaultLocale = this.setLanguage(defaultLocale); } setLanguage(locale) { const localeDef = this.config.locales.find(_locale => _locale.locale === locale); if (!localeDef) throw new ImplementationMissingException(`Unsupported language: ${locale}`); this.activeLocale = localeDef; dispatch_onLanguageChanged.dispatchUI(); this.selectedLanguage.set(localeDef.locale); return localeDef; } getAvailableLanguages() { return this.config.locales; } get(key, ...params) { let text = this.activeLocale.texts[key]; if (!text) text = this.defaultLocale.texts[key]; if (!text) return key; return format(text, ...params); } } export const LocaleModule = new LocaleModule_Class(); //# sourceMappingURL=LocaleModule.js.map