feat: Add language redirection
This commit is contained in:
parent
dc3dbb9f64
commit
673b293891
|
@ -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>
|
Loading…
Reference in New Issue