Files
LingvAI/utils/language.ts
David cf0b310918 Added hotkeys + updated dependencies & webpack5 + humanetech badge (#15)
* Added language & theme switch hotkeys

* Hotkeys testing

* Language switch when equal

* Shortcut keys changed

* Dependencies updated & upgraded to webpack5

* Added HumaneTech badge

* Disabled FLoC
2021-04-30 23:11:26 +02:00

44 lines
1.1 KiB
TypeScript

import languagesJson from "./languages.json";
const { languages, exceptions, mappings } = languagesJson;
const checkTypes = {
exception: exceptions,
mapping: mappings
};
export type CheckType = keyof typeof checkTypes;
const langTypes = [
"source",
"target"
] as const;
export type LangType = typeof langTypes[number];
const isKeyOf = <T extends object>(obj: T) => (key: keyof any): key is keyof T => key in obj;
export function replaceBoth(
checkType: CheckType,
langs: {
[key in LangType]: string
}
): {
[key in LangType]: string
} {
const [source, target] = langTypes.map(langType => {
const object = checkTypes[checkType][langType];
const langCode = langs[langType];
return isKeyOf(object)(langCode) ? object[langCode] : langCode;
});
return { source, target };
}
export function retrieveFiltered() {
const [sourceLangs, targetLangs] = langTypes.map(type => (
Object.entries(languages).filter(([code]) => (
!Object.keys(exceptions[type]).includes(code)
))
));
return { sourceLangs, targetLangs };
}