112 lines
3.2 KiB
JavaScript
112 lines
3.2 KiB
JavaScript
/**
|
|
* Flattens an object recursively. Nested keys are joined with slashes ('/')
|
|
* @param {object} obj The object to flatten
|
|
* @returns {object} The flattened object
|
|
*/
|
|
export function flattenObj(obj) {
|
|
const res = {}
|
|
Object.entries(obj).forEach(([key, value]) => {
|
|
if (typeof value === "object") {
|
|
value = flattenObj(value)
|
|
Object.entries(value).forEach(([key2, value2]) => {
|
|
res[key + "/" + key2] = value2
|
|
})
|
|
} else {
|
|
res[key] = value
|
|
}
|
|
})
|
|
return res
|
|
}
|
|
|
|
// Code (Flag): https://en.wikipedia.org/wiki/Regional_indicator_symbol
|
|
// Tag: https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes
|
|
export const LANGUAGES = {
|
|
"fre-ca": {
|
|
display: "Français CA",
|
|
code: "ca",
|
|
aliases: ["fr-ca", "vfq", "quebec", "québec", "ca", "canada"]
|
|
},
|
|
"fre": {
|
|
display: "Français FR",
|
|
code: "fr",
|
|
aliases: ["fr", "fre", "fra", "french", "francais", "français", "vf", "vff", "france"]
|
|
},
|
|
"eng": {
|
|
display: "English",
|
|
code: "gb",
|
|
aliases: ["en", "eng", "ang", "english", "anglais", "uk", "gb", "usa", "british", "american", "amérique", "amerique", "angleterre", "royaume-uni"]
|
|
},
|
|
"deu": {
|
|
display: "Deutsch",
|
|
code: "de",
|
|
aliases: ["de", "deu", "ger", "german", "allemand", "deutsch", "germany", "allemagne"]
|
|
},
|
|
"kor": {
|
|
display: "Korean",
|
|
code: "kr",
|
|
aliases: ["ko", "kr", "kor", "cor", "korean", "coreen", "coréen", "corée", "coree", "korea"]
|
|
},
|
|
"jpn": {
|
|
display: "Japanese",
|
|
code: "jp",
|
|
aliases: ["ja", "jp", "jap", "japanese", "japonais", "japon", "japan"]
|
|
},
|
|
"tur": {
|
|
display: "Turkish",
|
|
code: "tr",
|
|
aliases: ["tu", "tr", "tur", "tür", "turkish", "turc", "turquie"]
|
|
},
|
|
"und": {
|
|
display: "Undefined",
|
|
code: "",
|
|
aliases: []
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Tries to find a language name in the given string
|
|
* @param {string} value The string in which to search for a language
|
|
* @returns {?string} The language key if it could be determined, null otherwise
|
|
*/
|
|
export function findLanguage(value) {
|
|
for (const lang in LANGUAGES) {
|
|
const aliases = LANGUAGES[lang].aliases
|
|
const matches = aliases.map(a => {
|
|
return new RegExp("\\b" + a + "\\b").test(value)
|
|
})
|
|
if (matches.some(v => v)) {
|
|
return lang
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
export function isLanguageTagAlias(langTag, value) {
|
|
return LANGUAGES[langTag].aliases.includes(value)
|
|
}
|
|
|
|
export function updateObjectFromJoinedKey(obj, joinedKey, value) {
|
|
const keyParts = joinedKey.split("/")
|
|
for (const part of keyParts.slice(0, -1)) {
|
|
obj = obj[part]
|
|
}
|
|
obj[keyParts[keyParts.length - 1]] = value
|
|
}
|
|
|
|
/**
|
|
* @param {string} code
|
|
*/
|
|
export function makeFlag(code) {
|
|
return code.split("")
|
|
.map(c => String.fromCodePoint(
|
|
0x1f1e6 + c.codePointAt(0) - 97
|
|
))
|
|
.join("")
|
|
}
|
|
|
|
export function getLanguageOptions() {
|
|
return Object.entries(LANGUAGES).map(([tag, props]) => {
|
|
const flag = makeFlag(props.code)
|
|
return {value: tag, display: `${flag} ${props.display}`}
|
|
})
|
|
} |