🐛 Fix locale fallback mechanism (#1734)

This commit is contained in:
Luke Vella 2025-05-28 09:44:41 +01:00 committed by GitHub
parent eb625b946f
commit 53a957dc49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 16 deletions

View file

@ -1,14 +1,18 @@
import { match } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";
import languages, { defaultLocale } from "./index";
const locales = Object.keys(languages);
import { defaultLocale, supportedLngs } from "./index";
export function getPreferredLocale({
userLocale,
acceptLanguageHeader,
}: {
userLocale?: string;
acceptLanguageHeader?: string;
}) {
if (userLocale && supportedLngs.includes(userLocale)) {
return userLocale;
}
if (!acceptLanguageHeader) {
return defaultLocale;
}
@ -22,9 +26,8 @@ export function getPreferredLocale({
.filter((lang) => lang !== "*");
try {
return match(preferredLanguages, locales, defaultLocale);
return match(preferredLanguages, supportedLngs, defaultLocale);
} catch (e) {
console.warn("Failed to match locale", e);
return defaultLocale;
}
}