mapcomplete/UI/i18n/Locale.ts

39 lines
992 B
TypeScript
Raw Normal View History

import {UIEventSource} from "../../Logic/UIEventSource";
2020-07-25 16:00:08 +00:00
import {UIElement} from "../UIElement";
import {LocalStorageSource} from "../../Logic/Web/LocalStorageSource";
2020-10-27 00:01:34 +00:00
import {DropDown} from "../Input/DropDown";
2020-07-20 10:39:43 +00:00
export default class Locale {
2020-07-25 16:00:08 +00:00
2020-07-31 02:58:58 +00:00
public static language: UIEventSource<string> = Locale.setup();
2020-10-27 00:01:34 +00:00
2020-07-31 02:58:58 +00:00
private static setup() {
2020-07-31 15:11:44 +00:00
const source = LocalStorageSource.Get('language', "en");
if (!UIElement.runningFromConsole) {
// @ts-ignore
window.setLanguage = function (language: string) {
source.setData(language)
}
2020-07-31 02:58:58 +00:00
}
return source;
2020-07-25 16:00:08 +00:00
}
2020-07-31 02:58:58 +00:00
2020-10-27 00:01:34 +00:00
public static CreateLanguagePicker(
languages : string[] ,
label: string | UIElement = "") {
if (languages.length <= 1) {
return undefined;
}
return new DropDown(label, languages.map(lang => {
return {value: lang, shown: lang}
}
), Locale.language);
}
2020-07-20 10:39:43 +00:00
}
2020-07-25 16:00:08 +00:00