fix: build locale based on validated codes and regions (#11912)

* fix: build locale based on validated codes and regions

* keep html lang stable

* fix type error
This commit is contained in:
Greg Pabian authored and GitHub committed 2023-10-16 17:29:35 +01:00
1 parent bc81f659aa
commit f2ecd9818a
5 files changed
+324 -21

No files matched your search

+4
View File
@@ -33,6 +33,10 @@ const config = {
"zh-TW",
],
},
fallbackLng: {
default: ["en"],
zh: ["zh-CN"],
},
reloadOnPrerender: process.env.NODE_ENV !== "production",
};
+12 -1
View File
@@ -29,5 +29,16 @@ export const getLocale = async (req: GetTokenParams["req"]): Promise<string> =>
const languages = acceptLanguage ? parse(acceptLanguage) : [];
return languages[0]?.code || "en";
const code: string = languages[0]?.code ?? "";
const region: string = languages[0]?.region ?? "";
// the code should consist of 2 or 3 lowercase letters
// the regex underneath is more permissive
const testedCode = /^[a-zA-Z]+$/.test(code) ? code : "en";
// the code should consist of either 2 uppercase letters or 3 digits
// the regex underneath is more permissive
const testedRegion = /^[a-zA-Z0-9]+$/.test(region) ? region : "";
return `${testedCode}${testedRegion !== "" ? "-" : ""}${testedRegion}`;
};