website/templates/language_redirect.html
Alexander Borsuk 5cf6282a53 Stable sorted translations in the list
Signed-off-by: Alexander Borsuk <me@alex.bio>
2022-12-24 14:32:17 +01:00

24 lines
1.1 KiB
HTML

<script>
// Supports partially translated pages.
// Redirects a user to his preferred language page translation only if
// there were no previous redirects or explicit language selections before (see language_seletor.html)
if (null === window.localStorage.getItem('lang')) {
// ["en", "ru", ...]
var pageTranslations = {{ resource.translations | map(attribute="lang") | sort | json_encode() | safe }};
var userPreferredLanguages = navigator.languages ? navigator.languages : [navigator.language];
outerLoop:
for (var i = 0; i < userPreferredLanguages.length; i++) {
for (var j = 0; j < pageTranslations.length; j++) {
if (userPreferredLanguages[i].indexOf(pageTranslations[j]) == 0) {
window.localStorage.setItem('lang', pageTranslations[j]);
// Do not redirect the default 'en' language.
// TODO: Use config.default_language instead of hard-coding 'en' after Zola upgrade on CF.
if (pageTranslations[j] != 'en') {
window.location.pathname = '/' + pageTranslations[j] + window.location.pathname;
}
break outerLoop;
}
}
}
}
</script>