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"
|
2023-02-08 01:14:21 +01:00
|
|
|
import native from "../assets/language_native.json"
|
|
|
|
import language_translations from "../assets/language_translations.json"
|
2022-02-24 02:33:20 +01:00
|
|
|
import { Translation } from "./i18n/Translation"
|
2022-04-03 03:00:45 +02:00
|
|
|
import Lazy from "./Base/Lazy"
|
|
|
|
import Toggle from "./Input/Toggle"
|
2023-03-09 17:11:44 +01:00
|
|
|
import LanguageUtils from "../Utils/LanguageUtils"
|
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)
|
2023-03-09 17:11:44 +01:00
|
|
|
const allLanguages: string[] = LanguageUtils.usedLanguagesSorted
|
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
|
|
|
|
const translation = {}
|
2023-02-08 01:14:21 +01:00
|
|
|
const trans = language_translations[lang]
|
2022-07-03 00:30:11 +02:00
|
|
|
if (trans === undefined) {
|
2022-02-24 02:33:20 +01: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
|
|
|
|
}
|
2023-02-08 01:14:21 +01:00
|
|
|
const translationInKey = language_translations[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
|
|
|
}
|