mirror of
https://github.com/revoltchat/revite.git
synced 2024-12-26 23:42:11 -05:00
Fix issues with language detection
This commit is contained in:
parent
735e9b7f5e
commit
fbdbf5d0bc
1 changed files with 19 additions and 12 deletions
|
@ -53,16 +53,22 @@ 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";
|
|
||||||
|
|
||||||
|
if (preferredLanguage) {
|
||||||
// This moves the user's system language to the top of the language list
|
// This moves the user's system language to the top of the language list
|
||||||
const prefLangKey = languages.find((lang) => lang[0] == preferredLanguage);
|
const prefLangKey = languages.find(
|
||||||
|
(lang) => lang[0].replace(/_/g, "-") == preferredLanguage,
|
||||||
|
);
|
||||||
if (prefLangKey) {
|
if (prefLangKey) {
|
||||||
languages.splice(
|
languages.splice(
|
||||||
0,
|
0,
|
||||||
|
@ -70,6 +76,7 @@ export function Component(props: Props) {
|
||||||
languages.splice(languages.indexOf(prefLangKey), 1)[0],
|
languages.splice(languages.indexOf(prefLangKey), 1)[0],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.languages}>
|
<div className={styles.languages}>
|
||||||
|
|
Loading…
Reference in a new issue