Created
February 28, 2020 15:37
-
-
Save ashour/8747f191183eceef7a7b826c3a42c7de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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