feat: Add language redirection

This commit is contained in:
Simon 2023-05-17 16:44:49 +02:00
parent dc3dbb9f64
commit 673b293891
1 changed files with 68 additions and 0 deletions

68
layouts/alias.html Normal file
View File

@ -0,0 +1,68 @@
<!DOCTYPE html>
{{ $defaultLanguage := (index (first 1 (where site.Home.AllTranslations "Permalink" .Permalink)) 0).Language.Lang }}
<html lang="{{ $defaultLanguage }}">
<head>
<title>{{ i18n "title" }}</title>
<meta name="description" content="{{ i18n "description" }}">
<link rel="canonical" href="{{ .Permalink }}"/>
<meta name="robots" content="noindex">
<meta charset="utf-8"/>
<noscript>
<meta http-equiv="refresh" content="0; url={{ .Permalink }}"/>
</noscript>
<script>
;(function () {
// Only do i18n at root,
// otherwise, redirect immediately
if (window.location.pathname !== '/') {
window.location.replace('{{ .Permalink }}')
return
}
var getFirstBrowserLanguage = function () {
var nav = window.navigator,
browserLanguagePropertyKeys = ['language', 'browserLanguage', 'systemLanguage', 'userLanguage'],
i,
language
if (Array.isArray(nav.languages)) {
for (i = 0; i < nav.languages.length; i++) {
language = nav.languages[i]
if (language && language.length) {
return language
}
}
}
// support for other well known properties in browsers
for (i = 0; i < browserLanguagePropertyKeys.length; i++) {
language = nav[browserLanguagePropertyKeys[i]]
if (language && language.length) {
return language
}
}
return '{{ $defaultLanguage }}'
}
var preferLang = getFirstBrowserLanguage()
{{ range $index, $elem := site.Home.AllTranslations }}
{{ if ne $index 0 }}else {{end}}if (preferLang.indexOf('{{ $elem.Lang }}') !== -1) {
window.location.replace('{{ $elem.Permalink }}')
}
{{ end }}
else {
{{- range site.Home.AllTranslations -}}
{{- if eq $defaultLanguage .Lang }}
window.location.replace('{{ .Permalink }}')
{{ end -}}
{{ end -}}
}
})()
</script>
</head>
<body>
<h1>{{ i18n "title" }}</h1>
<p>You should be rerouted in a jiff, if not, <a href="{{ .Permalink }}">click here</a>.</p>
</body>
</html>