2020-11-05 13:34:48 +01:00
|
|
|
import { DropDown } from "./Input/DropDown"
|
|
|
|
import Locale from "./i18n/Locale"
|
2021-06-12 02:58:32 +02:00
|
|
|
import BaseUIElement from "./BaseUIElement"
|
2022-02-24 02:33:20 +01:00
|
|
|
import * as native from "../assets/language_native.json"
|
|
|
|
import * as language_translations from "../assets/language_translations.json"
|
|
|
|
import { Translation } from "./i18n/Translation"
|
2022-04-03 03:00:45 +02:00
|
|
|
import * as used_languages from "../assets/generated/used_languages.json"
|
|
|
|
import Lazy from "./Base/Lazy"
|
|
|
|
import Toggle from "./Input/Toggle"
|
2020-11-05 13:34:48 +01:00
|
|
|
|
2022-07-03 00:30:11 +02:00
|
|
|
export default class LanguagePicker extends Toggle {
|
|
|
|
constructor(languages: string[], label: string | BaseUIElement = "") {
|
2022-09-27 18:48:02 +02:00
|
|
|
console.log("Constructing a language pîcker for languages", languages)
|
2021-11-30 21:29:17 +01:00
|
|
|
if (languages === undefined || languages.length <= 1) {
|
2022-07-25 18:55:15 +02:00
|
|
|
super(undefined, undefined, undefined)
|
2022-10-27 01:50:01 +02:00
|
|
|
} else {
|
2022-09-27 18:48:02 +02:00
|
|
|
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
|
2020-11-05 13:34:48 +01:00
|
|
|
}
|
2022-04-03 03:00:45 +02:00
|
|
|
}
|
2022-07-03 00:30:11 +02:00
|
|
|
|
2022-04-03 03:00:45 +02:00
|
|
|
private static dropdownFor(languages: string[], label: string | BaseUIElement): BaseUIElement {
|
2022-07-03 00:30:11 +02:00
|
|
|
return new DropDown(
|
|
|
|
label,
|
|
|
|
languages
|
|
|
|
.filter((lang) => lang !== "_context")
|
|
|
|
.map((lang) => {
|
|
|
|
return { value: lang, shown: LanguagePicker.hybrid(lang) }
|
|
|
|
}),
|
|
|
|
Locale.language
|
|
|
|
)
|
2020-11-05 13:34:48 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 02:33:20 +01:00
|
|
|
private static hybrid(lang: string): Translation {
|
|
|
|
const nativeText = native[lang] ?? lang
|
2022-07-03 00:30:11 +02:00
|
|
|
const allTranslations = language_translations["default"] ?? language_translations
|
2022-02-24 02:33:20 +01:00
|
|
|
const translation = {}
|
2022-07-03 00:30:11 +02:00
|
|
|
const trans = allTranslations[lang]
|
|
|
|
if (trans === undefined) {
|
2022-02-24 02:33:20 +01:00
|
|
|
return new Translation({ "*": nativeText })
|
|
|
|
}
|
|
|
|
for (const key in trans) {
|
|
|
|
const translationInKey = allTranslations[lang][key]
|
2022-07-03 00:30:11 +02:00
|
|
|
if (nativeText.toLowerCase() === translationInKey.toLowerCase()) {
|
2022-02-24 02:33:20 +01:00
|
|
|
translation[key] = nativeText
|
2022-07-03 00:30:11 +02:00
|
|
|
} else {
|
|
|
|
translation[key] = nativeText + " (" + translationInKey + ")"
|
2022-02-24 02:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Translation(translation)
|
2022-07-03 00:30:11 +02:00
|
|
|
}
|
2020-11-05 13:34:48 +01:00
|
|
|
}
|