refactor: use locale instead of two letter code

This commit is contained in:
Elias Schneider
2023-07-22 13:08:42 +02:00
parent e6a2014875
commit 890588f5da
11 changed files with 25 additions and 2257 deletions

View File

@@ -11,13 +11,21 @@ const getLanguageFromAcceptHeader = (acceptLanguage?: string) => {
const languages = acceptLanguage.split(",").map((l) => l.split(";")[0]);
const supportedLanguages = Object.values(LOCALES).map((l) => l.code);
const supportedLanguagesWithoutRegion = supportedLanguages.map(
(l) => l.split("-")[0]
);
for (const language of languages) {
// Try to match the full language code first, then the language code without the region
if (supportedLanguages.includes(language)) {
return language;
} else if (supportedLanguages.includes(language.split("-")[0])) {
return language.split("-")[0];
} else if (
supportedLanguagesWithoutRegion.includes(language.split("-")[0])
) {
const similarLanguage = supportedLanguages.find((l) =>
l.startsWith(language.split("-")[0])
);
return similarLanguage;
}
}
return "en";