-
-
Save nocodesupplyco/98258311c55c1a0ff6d30425b1e299d9 to your computer and use it in GitHub Desktop.
Detect Spanish Language and Redirect
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
(function () { | |
//https://github.com/maxogden/browser-locale/blob/master/index.js | |
var browserLocale = function () { | |
var lang; | |
if (navigator.languages && navigator.languages.length) { | |
// latest versions of Chrome and Firefox set this correctly | |
lang = navigator.languages[0]; | |
} else if (navigator.userLanguage) { | |
// IE only | |
lang = navigator.userLanguage; | |
} else { | |
// latest versions of Chrome, Firefox, and Safari set this correctly | |
lang = navigator.language; | |
} | |
return lang; | |
}; | |
document.addEventListener("DOMContentLoaded", function (event) { | |
var lang = browserLocale().slice(0, 2); | |
// redirect if not the right locale | |
if (lang === "es" && !location.search.match(/locale=en/i)) { | |
window.location = "/es"; | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment