Last active
February 27, 2018 23:17
-
-
Save 2aces/2d06c06b7b1fc0f594cf4ca8696a152e to your computer and use it in GitHub Desktop.
Autoptimize / Optimize Matters website language switcher
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
// includes polyfill https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes | |
Array.prototype.includes||Object.defineProperty(Array.prototype,"includes",{value:function(r,e){if(null==this)throw new TypeError('"this" is null or not defined');var t=Object(this),n=t.length>>>0;if(0===n)return!1;var i,o,a=0|e,u=Math.max(a>=0?a:n-Math.abs(a),0);for(;u<n;){if((i=t[u])===(o=r)||"number"==typeof i&&"number"==typeof o&&isNaN(i)&&isNaN(o))return!0;u++}return!1}}); | |
(function() { | |
var httpRequest; | |
var location = window.location.href; | |
var suffix = ''; | |
httpRequest = new XMLHttpRequest(); | |
httpRequest.onreadystatechange = getCountry; | |
httpRequest.open('GET', 'https://freegeoip.net/json/', true); | |
httpRequest.send(); | |
function getCountry() { | |
if (httpRequest.readyState === XMLHttpRequest.DONE) { | |
if (httpRequest.status === 200) { | |
langCountryMap = ['pt','es']; | |
langCountryMap['pt'] = ['br','pt','ao','mz','cv']; | |
langCountryMap['es'] = ['es','ar','cl','co','mx','bo','ve','pe','ec','gt','cu','do','hn','py','sv','ni','cr','pr','pa','uy','gq']; | |
// ['en'] it's the default, we don't need to care for it | |
// langCountryMap['en'] = ['us','gb','ca','au','nz']; | |
countryCode = JSON.parse(httpRequest.responseText).country_code.toLowerCase(); | |
browserLang = navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage; | |
browserLang = browserLang.substring(0,2).toLowerCase(); | |
// check if we already are in the proper url for english | |
// if we are, we bail early and avoid a a redirect with window.location.href | |
if ( location === 'https://autoptimize.com/' && ( countryCode == 'us' || browserLang == 'us') ) { | |
return; | |
} | |
// check if we already are in the proper url for other languages than english | |
// if we are, we bail early and avoid a a redirect with window.location.href | |
if ( location.indexOf( '/' + countryCode + '/' ) != -1 || location.indexOf( '/' + browserLang + '/' ) != -1) { | |
return; | |
} | |
langCountryMap.forEach(function(element) { | |
if (langCountryMap[element].includes(countryCode) || browserLang == element) { | |
suffix = element; | |
return; | |
} | |
}) | |
window.location.href = 'https://autoptimize.com/' + suffix; | |
} | |
} | |
} | |
})(); |
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
// includes polyfill https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes | |
Array.prototype.includes||Object.defineProperty(Array.prototype,"includes",{value:function(r,e){if(null==this)throw new TypeError('"this" is null or not defined');var t=Object(this),n=t.length>>>0;if(0===n)return!1;var i,o,a=0|e,u=Math.max(a>=0?a:n-Math.abs(a),0);for(;u<n;){if((i=t[u])===(o=r)||"number"==typeof i&&"number"==typeof o&&isNaN(i)&&isNaN(o))return!0;u++}return!1}}); | |
(function() { | |
var httpRequest; | |
var location = window.location.href; | |
var suffix = ''; | |
httpRequest = new XMLHttpRequest(); | |
httpRequest.onreadystatechange = getCountry; | |
httpRequest.open('GET', 'https://freegeoip.net/json/', true); | |
httpRequest.send(); | |
function getCountry() { | |
if (httpRequest.readyState === XMLHttpRequest.DONE) { | |
if (httpRequest.status === 200) { | |
langCountryMap = ['pt','es']; | |
langCountryMap['pt'] = ['br','pt','ao','mz','cv']; | |
langCountryMap['es'] = ['es','ar','cl','co','mx','bo','ve','pe','ec','gt','cu','do','hn','py','sv','ni','cr','pr','pa','uy','gq']; | |
countryCode = JSON.parse(httpRequest.responseText).country_code.toLowerCase(); | |
browserLang = navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage; | |
browserLang = browserLang.substring(0,2).toLowerCase(); | |
if ( location === 'https://autoptimize.com/' && ( countryCode == 'us' || browserLang == 'us') ) { | |
return; | |
} | |
if ( location.indexOf( '/' + countryCode + '/' ) != -1 || location.href.indexOf( '/' + browserLang + '/' ) != -1) { | |
return; | |
} | |
langCountryMap.forEach(function(element) { | |
if (langCountryMap[element].includes(countryCode) || browserLang == element) { | |
suffix = element; | |
return; | |
} | |
}) | |
window.location.href = 'https://autoptimize.com/' + suffix; | |
} | |
} | |
} | |
})(); |
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
// includes polyfill https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes | |
Array.prototype.includes||Object.defineProperty(Array.prototype,"includes",{value:function(r,e){if(null==this)throw new TypeError('"this" is null or not defined');var t=Object(this),n=t.length>>>0;if(0===n)return!1;var i,o,a=0|e,u=Math.max(a>=0?a:n-Math.abs(a),0);for(;u<n;){if((i=t[u])===(o=r)||"number"==typeof i&&"number"==typeof o&&isNaN(i)&&isNaN(o))return!0;u++}return!1}}); | |
(function() { | |
var httpRequest; | |
httpRequest = new XMLHttpRequest(); | |
httpRequest.onreadystatechange = getCountry; | |
httpRequest.open('GET', 'https://freegeoip.net/json/', true); | |
httpRequest.send(); | |
function getCountry() { | |
if (httpRequest.readyState === XMLHttpRequest.DONE) { | |
if (httpRequest.status === 200) { | |
langCountryMap = ['pt','es','en']; | |
langCountryMap['pt'] = ['br','pt','ao','mz','cv']; | |
langCountryMap['es'] = ['es','ar','cl','co','mx','bo','ve','pe','ec','gt','cu','do','hn','py','sv','ni','cr','pr','pa','uy','gq']; | |
langCountryMap['en'] = ['us','gb','ca','au','nz']; | |
countryCode = JSON.parse(httpRequest.responseText).country_code.toLowerCase(); | |
browserLang = navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage; | |
browserLang = browserLang.substring(0,2).toLowerCase(); | |
langCountryMap.forEach(function(element) { | |
if (langCountryMap[element].includes(countryCode) || browserLang == element) { | |
if (element == 'en') element=''; | |
window.location.href = "https://autoptimize.com/"+element; | |
} | |
}) | |
// default aka fallback: EN | |
if (window.location.href != "https://autoptimize.com/") { | |
window.location.href = "https://autoptimize.com/"; | |
} | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment