Created
February 27, 2020 02:14
-
-
Save yunseop-kim/0ed3643067fa27112de244ac4155fe55 to your computer and use it in GitHub Desktop.
detect user's language
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
<script | |
src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js" | |
integrity="sha256-VeNaFBVDhoX3H+gJ37DpT/nTuZTdjYro9yBruHjVmoQ=" | |
crossorigin="anonymous" | |
></script> | |
</head> | |
<body> | |
<script> | |
const supportedLang = [ | |
{ code: "zh-TW", path: "/", identifier: "tw" }, | |
{ code: "zh-HK", path: "/hk", identifier: "hk" }, | |
{ code: "vi", path: "/vi", identifier: "vi" }, | |
{ code: "en", path: "/en", identifier: "en" }, | |
{ code: "jp", path: "/jp", identifier: "jp" }, | |
{ code: "th", path: "/th", identifier: "th" } | |
]; | |
const browserLanguages = ( | |
navigator.languages || [navigator.language] | |
).map(lang => lang.toLowerCase()); | |
const result = _.intersectionWith( | |
supportedLang, | |
browserLanguages, | |
(supported, browser) => | |
_.isString(browser) && browser.includes(supported.identifier) | |
); | |
console.log(result); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment