4
| <![CDATA[<script>
var storedLocale = localStorage.getItem('locale');
var locale = getLocale();
localStorage.setItem('locale', locale);
// Redirect to locale
var href = getLocaleUrl(locale);
location.href = href;
function getLocaleUrl(locale) {
var href = location.href;
if (storedLocale && locale !== storedLocale && href.indexOf('/' + storedLocale + '/') > -1) {
href = href.replace('/' + storedLocale + '/', '/' + locale + '/');
} else {
href = location.href.replace(/\/[^\/]*$/, '') + '/' + locale.replace(/^\//, '').replace(/\/$/, '') + '/'; // index.html
href += (location.search !== '' ? '?' + location.search.replace(/^\?/, '') : '');
}
return href;
}
function getLocale() {
var locale = navigator.language || navigator.browserLanguage;
// Override with locale from the document
var documentLocale = document['locale'];
if (documentLocale) {
locale = documentLocale;
}
// Override with stored locale
var storedLocale = localStorage.getItem('locale');
if (storedLocale) {
locale = storedLocale;
}
// Override with locale from URL query string
var localeParam = getUrlParameter('locale');
if (localeParam) {
locale = localeParam;
}
// Format locale properly
locale = locale.replace(/-.+/g, function(p1) {
return p1.toUpperCase();
});
// Fix en-US and default to en
if (!locale) {
locale = 'en';
} else
if (locale.indexOf('pt') === 0) {
locale = 'pt-BR';
} else
if (locale.indexOf('en') === 0) {
locale = 'en';
} else
if (locale.indexOf('es') === 0) {
locale = 'es';
}
return locale;
}
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null;
}
</script>]]>
|