Modify language identification in locale handling

The update changes the way locales are checked against the list of accepted languages. Now, instead the full locale, only the language segment is considered when determining if the locale is accepted. Additionally, a check in 'getPreferredLanguageFromBrowser' was added to ensure 'acceptLanguage' is not undefined before attempting to parse it.
This commit is contained in:
giancarlo
2024-04-23 23:04:17 +07:00
parent 448fee01c4
commit a004cbae63
2 changed files with 11 additions and 1 deletions

View File

@@ -58,6 +58,10 @@ export const createI18nServerInstance = cache(createInstance);
function getPreferredLanguageFromBrowser() {
const acceptLanguage = headers().get('accept-language');
if (!acceptLanguage) {
return;
}
return parseAcceptLanguageHeader(acceptLanguage, languages)[0];
}

View File

@@ -73,9 +73,15 @@ export function parseAcceptLanguageHeader(
// Ignore wildcard '*' if 'ignoreWildcard' is true
if (locale === '*' && ignoreWildcard) return [];
const languageSegment = locale.split('-')[0];
if (!languageSegment) return [];
// Return the locale if it's included in the accepted languages
try {
return acceptedLanguages.includes(locale) ? [locale] : [];
return acceptedLanguages.includes(languageSegment)
? [languageSegment]
: [];
} catch {
return [];
}