Skip to content

Instantly share code, notes, and snippets.

@nocodesupplyco
Created December 19, 2022 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nocodesupplyco/98258311c55c1a0ff6d30425b1e299d9 to your computer and use it in GitHub Desktop.
Save nocodesupplyco/98258311c55c1a0ff6d30425b1e299d9 to your computer and use it in GitHub Desktop.
Detect Spanish Language and Redirect
(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