Skip to content

Instantly share code, notes, and snippets.

@ashour
Created February 28, 2020 15:37
Show Gist options
  • Save ashour/8747f191183eceef7a7b826c3a42c7de to your computer and use it in GitHub Desktop.
Save ashour/8747f191183eceef7a7b826c3a42c7de to your computer and use it in GitHub Desktop.
export const defaultLang = "en";
export const supportedLangs = {
en: "English",
ar: "Arabic (عربي)",
};
export function determineUserLang(acceptedLangs) {
const acceptedLangCodes = acceptedLangs.map(stripCountry);
const supportedLangCodes = Object.keys(supportedLangs);
const matchingLangCode = acceptedLangCodes.find(code =>
supportedLangCodes.includes(code),
);
return matchingLangCode || defaultLang;
}
function stripCountry(lang) {
return lang
.trim()
.replace("_", "-")
.split("-")[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment