Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a langauge-based js redirect to the homepage #60

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ paginate = 6
[languages.de]
weight = 1
languageName = "Deutsch"
languageCode = "de"
calendarName = "Kalender"
contentDir = "content/de"
title = "xHain hack+makespace"
Expand All @@ -50,6 +51,7 @@ defaultDescription = "Hack- & Makespace im Herzen von Friedrichshain, Bastelstub
[languages.en]
weight = 2
languageName = "English"
languageCode = "en"
calendarName = "Calendar"
contentDir = "content/en"
title = "xHain hack+makespace"
Expand Down
31 changes: 31 additions & 0 deletions layouts/alias.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="{{ site.Language.LanguageCode }}">
<head>
<title>{{ .Permalink }}</title>
<link rel="canonical" href="{{ .Permalink }}">
<meta name="robots" content="noindex">
<meta charset="utf-8">
</head>
{{/* If its the homepage, the target will end with just the /de/, all other will have a longer url */}}
{{ if hasSuffix .Permalink "/de/" }}
<noscript>
<meta http-equiv="refresh" content="0; url={{ .Permalink }}"/>
</noscript>
<script type="text/javascript">
const siteLangs = [{{ range site.Languages }} '{{ .LanguageCode }}', {{ end }}];

function findLang() {
for (i in navigator.languages) {
let lang = navigator.languages[i].split('-')[0];
if (siteLangs.includes(lang)) {
return lang;
}
}
return 'en';
}
window.location.replace(`/${findLang()}/`);
</script>
{{ else }}
<meta http-equiv="refresh" content="0; url={{ .Permalink }}"/>
{{ end }}
</html>