Automatic language redirect
Signed-off-by: Alexander Borsuk <me@alex.bio>
This commit is contained in:
parent
1a7eb10a4c
commit
e17aa809f0
4 changed files with 38 additions and 4 deletions
|
@ -57,6 +57,11 @@
|
|||
<link href="/main.css?h={{ get_file_hash(path='sass/main.scss', sha_type=256, base64=false) }}" rel="stylesheet" type="text/css">
|
||||
|
||||
<title>{{ resource.title }}</title>
|
||||
|
||||
{# TODO: Replace 'en' with config.default_language after upgrading Zola #}
|
||||
{%- if lang == 'en' %}
|
||||
{% include 'language_redirect.html' %}
|
||||
{% endif -%}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
@ -65,7 +70,7 @@
|
|||
</header>
|
||||
|
||||
<main>
|
||||
{% block content %} {% endblock %}
|
||||
{% block content %}{% endblock %}
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
|
|
24
templates/language_redirect.html
Normal file
24
templates/language_redirect.html
Normal file
|
@ -0,0 +1,24 @@
|
|||
<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") | 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>
|
|
@ -1,15 +1,21 @@
|
|||
<span>
|
||||
<script>
|
||||
function onLanguageClick(langCode) {
|
||||
window.localStorage.setItem('lang', langCode);
|
||||
return true;
|
||||
}
|
||||
</script>
|
||||
<!-- Hidden checkbox is used for pure CSS toggle menu. -->
|
||||
<input type="checkbox" id="menu_trigger" class="menu_trigger" />
|
||||
<label class="no-print" for="menu_trigger">
|
||||
🌐 {{ lang | upper }} {# trans(key="language", lang=lang) #}
|
||||
🌐 {{ lang | upper }}
|
||||
</label>
|
||||
|
||||
<ul class="menu no-print" role="navigation">
|
||||
{% for translation in resource.translations %}
|
||||
{% if lang != translation.lang %}
|
||||
<li class="menu__item" role="menuitem">
|
||||
<a class="menu__link" href="{{ translation.permalink }}">
|
||||
<a class="menu__link" onclick="return onLanguageClick('{{translation.lang}}');" href="{{ translation.permalink }}">
|
||||
{{ trans(key="language", lang=translation.lang) }}
|
||||
</a>
|
||||
</li>
|
||||
|
|
|
@ -41,5 +41,4 @@
|
|||
// Disable two unpopular methods to keep the number of methods constant
|
||||
document.getElementById('sofort').style.display = 'none';
|
||||
}
|
||||
//
|
||||
</script>
|
||||
|
|
Loading…
Add table
Reference in a new issue