Fix issues with language detection

This commit is contained in:
janderedev 2021-08-06 22:12:10 +02:00
parent 735e9b7f5e
commit fbdbf5d0bc
No known key found for this signature in database
GPG key ID: 5D5E18ACB990F57A

View file

@ -53,22 +53,29 @@ export function Component(props: Props) {
Langs[x as keyof typeof Langs], Langs[x as keyof typeof Langs],
]) as Key[]; ]) as Key[];
// Get the user's system language // Get the user's system language. Check for exact
// matches first, otherwise check for partial matches
const preferredLanguage = const preferredLanguage =
navigator.languages.filter((lang) =>
languages.find((l) => l[0].replace(/_/g, "-") == lang),
)?.[0] ||
navigator.languages navigator.languages
?.filter((lang) => ?.map((x) => x.split("-")[0])
languages.find((l) => l[0] == lang.split("-")[0]), ?.filter((lang) => languages.find((l) => l[0] == lang))?.[0]
)?.[0] ?.split("-")[0];
?.split("-")[0] ?? "en";
// This moves the user's system language to the top of the language list if (preferredLanguage) {
const prefLangKey = languages.find((lang) => lang[0] == preferredLanguage); // This moves the user's system language to the top of the language list
if (prefLangKey) { const prefLangKey = languages.find(
languages.splice( (lang) => lang[0].replace(/_/g, "-") == preferredLanguage,
0,
0,
languages.splice(languages.indexOf(prefLangKey), 1)[0],
); );
if (prefLangKey) {
languages.splice(
0,
0,
languages.splice(languages.indexOf(prefLangKey), 1)[0],
);
}
} }
return ( return (