mapcomplete/UI/LanguagePicker.ts

58 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import { DropDown } from "./Input/DropDown"
import Locale from "./i18n/Locale"
import BaseUIElement from "./BaseUIElement"
import * as native from "../assets/language_native.json"
import * as language_translations from "../assets/language_translations.json"
2022-09-08 21:40:48 +02:00
import { Translation } from "./i18n/Translation"
import * as used_languages from "../assets/generated/used_languages.json"
2022-09-08 21:40:48 +02:00
import Lazy from "./Base/Lazy"
import Toggle from "./Input/Toggle"
export default class LanguagePicker extends Toggle {
2022-09-08 21:40:48 +02:00
constructor(languages: string[], label: string | BaseUIElement = "") {
console.log("Constructing a language pîcker for languages", languages)
if (languages === undefined || languages.length <= 1) {
2022-09-08 21:40:48 +02:00
super(undefined, undefined, undefined)
2022-10-27 01:50:01 +02:00
} else {
const normalPicker = LanguagePicker.dropdownFor(languages, label)
const fullPicker = new Lazy(() => LanguagePicker.dropdownFor(allLanguages, label))
super(fullPicker, normalPicker, Locale.showLinkToWeblate)
const allLanguages: string[] = used_languages.languages
}
}
private static dropdownFor(languages: string[], label: string | BaseUIElement): BaseUIElement {
2022-09-08 21:40:48 +02:00
return new DropDown(
label,
languages
.filter((lang) => lang !== "_context")
.map((lang) => {
return { value: lang, shown: LanguagePicker.hybrid(lang) }
}),
Locale.language
)
}
private static hybrid(lang: string): Translation {
const nativeText = native[lang] ?? lang
2022-09-08 21:40:48 +02:00
const allTranslations = language_translations["default"] ?? language_translations
const translation = {}
const trans = allTranslations[lang]
if (trans === undefined) {
2022-09-08 21:40:48 +02:00
return new Translation({ "*": nativeText })
}
for (const key in trans) {
2022-11-02 13:47:34 +01:00
if (key.startsWith("_")) {
2022-10-29 03:02:42 +02:00
continue
}
const translationInKey = allTranslations[lang][key]
if (nativeText.toLowerCase() === translationInKey.toLowerCase()) {
translation[key] = nativeText
} else {
translation[key] = nativeText + " (" + translationInKey + ")"
}
}
return new Translation(translation)
}
2022-09-08 21:40:48 +02:00
}