51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
export async function getSupportedLanguages() {
|
|
const metadata: [] = await (await fetch('http://localhost:4321/lang/metadata.json')).json();
|
|
|
|
const response: Array<string> = [];
|
|
|
|
metadata.forEach((lang: any) => {
|
|
response.push(lang.id);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
export async function getHighestWeightedLanguage(header: string | null): Promise<any> {
|
|
if(header === null) return 'en';
|
|
const supportedLanguages = await getSupportedLanguages();
|
|
|
|
const langs = header.split(',');
|
|
let highestWeight = 0;
|
|
let highestWeightedLang = 'en';
|
|
|
|
for(const lang of langs) {
|
|
const subArray = lang.split(";");
|
|
const langName = subArray[0];
|
|
const langWeight = subArray.filter((sub: string) => sub.includes("q=")).length > 0 ? parseFloat(subArray.filter((sub: string) => sub.includes("q="))[0].split("=")[1]) : 1;
|
|
|
|
if(langWeight > highestWeight && supportedLanguages.includes(langName)) {
|
|
highestWeight = langWeight;
|
|
highestWeightedLang = langName;
|
|
}
|
|
}
|
|
|
|
return highestWeightedLang;
|
|
}
|
|
|
|
export async function getLocales(language: string, type: string) {
|
|
if(!(await getSupportedLanguages()).includes(language)) {
|
|
console.log(await getSupportedLanguages(), language)
|
|
return null;
|
|
}
|
|
|
|
const lang = await (await fetch(`http://localhost:4321/lang/${language}/${type}.json`)).json();
|
|
const out: any = {};
|
|
|
|
for(const category in lang) {
|
|
for(const element in lang[category]) {
|
|
out[`${category}_${element}`] = lang[category][element];
|
|
}
|
|
}
|
|
|
|
return out;
|
|
} |