mapcomplete/UI/Popup/AllLanguagesSelector.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-11-02 14:44:06 +01:00
import { SearchablePillsSelector } from "../Input/SearchableMappingsSelector"
import { Store } from "../../Logic/UIEventSource"
import BaseUIElement from "../BaseUIElement"
import * as all_languages from "../../assets/language_translations.json"
import { Translation } from "../i18n/Translation"
2022-11-02 14:44:06 +01:00
export class AllLanguagesSelector extends SearchablePillsSelector<string> {
constructor(options?: {
2022-11-02 14:44:06 +01:00
mode?: "select-many" | "select-one"
currentCountry?: Store<string>
supportedLanguages?: Record<string, string> & { _meta?: { countries?: string[] } }
}) {
const possibleValues: {
show: BaseUIElement
value: string
mainTerm: Record<string, string>
2022-11-02 14:44:06 +01:00
searchTerms?: Record<string, string[]>
hasPriority?: Store<boolean>
}[] = []
const langs = options?.supportedLanguages ?? all_languages["default"] ?? all_languages
for (const ln in langs) {
2022-11-02 14:44:06 +01:00
let languageInfo: Record<string, string> & { _meta?: { countries: string[] } } =
all_languages[ln]
const countries = languageInfo._meta?.countries?.map((c) => c.toLowerCase())
languageInfo = { ...languageInfo }
delete languageInfo._meta
const term = {
show: new Translation(languageInfo),
value: ln,
mainTerm: languageInfo,
2022-11-02 14:44:06 +01:00
searchTerms: { "*": [ln] },
hasPriority:
countries === undefined
? undefined
: options?.currentCountry?.map(
(country) => countries?.indexOf(country.toLowerCase()) >= 0
),
}
possibleValues.push(term)
}
2022-11-02 14:44:06 +01:00
super(possibleValues, {
mode: options?.mode ?? "select-many",
})
}
}