The original and ugly code:
const lang = navigator.language || navigator.userLanguage;
if (lang.indexOf("de") == 0)
{
window.location.href = 'https://gusbemacbe.github.io/suru-plus-folders/de/';
}
# ...
else if (lang.indexOf("es") == 0)
{
window.location.href = 'https://gusbemacbe.github.io/suru-plus-folders/es/';
}
else
{
window.location.href = 'https://gusbemacbe.github.io/suru-plus-folders/en/';
}
I simplified into an elegant condition-based operation:
const lang = navigator.language || navigator.userLanguage;
const url = 'https://gusbemacbe.github.io/suru-plus-folders/'
function detection(lang)
{
let languages =
{
'de': () => window.location.href = url+'de',
'es': () => window.location.href = url+'es',
'fr': () => window.location.href = url+'fr',
'it': () => window.location.href = url+'it',
'nl': () => window.location.href = url+'nl',
'pt-BR': () => window.location.href = url+'pt-br',
'pt-PT': () => window.location.href = url+'pt-pt'
}
return languages(lang);
}
detection(lang);
The problem is else
, that is for needing to trigger to English if there is not another language into which I didn't translate the website. I checked that I could return languages(lang) ? 'en': () => window.location.href = url+'en'
, but I am not sure that it is right. I also thought of adding en': () => window.location.href = url+'en'
to let languages
, but it needs to be an "else
".