Skip to content

Commit

Permalink
Merge pull request #1091 from d-oit/main
Browse files Browse the repository at this point in the history
Save and load the selected language on domain level
  • Loading branch information
markdumay authored Aug 13, 2024
2 parents b9bce48 + a66dc2f commit 5dd0fce
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 10 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
assets/js/critical/languageSelector.js
assets/js/critical/color.js
assets/js/analytics.js
assets/js/flexsearch.js
Expand Down
65 changes: 65 additions & 0 deletions assets/js/critical/languageSelector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* eslint-disable */
{{- if site.Params.main.enableLanguageSelectionStorage -}}

(() => {
'use strict'

// Function to get the selected language from localStorage
function getLanguage () {
return localStorage.getItem('selectedLanguage')
}

// Function to set the selected language in localStorage
function setLanguage (language) {
localStorage.setItem('selectedLanguage', language)
}

// Function to apply the selected language to the website
function applyLanguage (language, href) {
if (document.documentElement.lang !== language) {
if (href) {
if (window.location.pathname !== href) {
window.location.href = href
}
} else {
window.location.href = '/' + language + '/'
}
}
}

// Event listener for language selection
document.addEventListener('DOMContentLoaded', () => {
const storedLanguage = getLanguage()
const languageItems = document.querySelectorAll('#language-selector .dropdown-item')

if (languageItems.length > 0) {
// Redirect if the stored language differs from the active language
if ((storedLanguage) && (document.documentElement.lang !== storedLanguage)) {
languageItems.forEach(item => {
if (item.getAttribute('hreflang') === storedLanguage) {
applyLanguage(storedLanguage, item.getAttribute('href'))
}
})
}

// Update the stored language when the user selects a new one
languageItems.forEach(item => {
item.addEventListener('click', () => {
const selectedLanguage = item.getAttribute('hreflang')

if (selectedLanguage) {
setLanguage(selectedLanguage)
}
})
})
}
else {
// Redirect to the localized homepage
const defaultLang = '{{ site.LanguageCode | default site.Language.Lang }}'
let language = storedLanguage ? storedLanguage : defaultLang
window.location.href = '/' + language + '/'
}
})
})()
{{- end -}}
/* eslint-enable */
1 change: 1 addition & 0 deletions config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
separator = "-"
description = "Hinode is a clean documentation and blog theme for your Hugo site based on Bootstrap 5."
enableDarkMode = true
enableLanguageSelectionStorage = false
modes = ["light", "dark"]
canonifyAssetsURLs = false
endorse = true
Expand Down
3 changes: 2 additions & 1 deletion exampleSite/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/_vendor
_vendor/
exampleSite/public/
public/
resources/

Expand Down
1 change: 1 addition & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
separator = "-"
description = "Hinode is a clean documentation and blog theme for your Hugo site based on Bootstrap 5."
enableDarkMode = true
enableLanguageSelectionStorage = true
modes = ["light", "dark"]
canonifyAssetsURLs = false
footerBelowFold = false
Expand Down
1 change: 1 addition & 0 deletions exampleSite/hugo_stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@
"indicateur-de-chargement",
"infobulle",
"invite-de-commandes",
"language-selector",
"leaflet-map-0",
"lien",
"link",
Expand Down
6 changes: 5 additions & 1 deletion layouts/alias.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<meta name="robots" content="noindex">
<meta charset="utf-8">
<noscript><meta http-equiv="refresh" content="0; url={{ .Permalink }}"></noscript>
<script src='{{ partial "utilities/GetStaticURL" (dict "url" "js/alias.js") }}'></script>
{{ if site.Params.main.enableLanguageSelectionStorage }}
{{- partial "footer/scripts.html" (dict "filename" "js/critical.bundle.js" "match" "js/critical/**.js" "header" true "page" page) -}}
{{ else }}
<script src='{{ partial "utilities/GetStaticURL" (dict "url" "js/alias.js") }}'></script>
{{ end }}
</head>
</html>
6 changes: 3 additions & 3 deletions layouts/partials/assets/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@
<a class="nav-link dropdown-toggle d-none d-{{ $size }}-block" href="#!" role="button" data-bs-toggle="dropdown" aria-label="{{ T "languageSwitcherLabel" }}" aria-expanded="false">
{{- partial "assets/icon.html" (dict "icon" "fas globe fa-fw" "spacing" false) }}
</a>
<ul class="dropdown-menu dropdown-menu-end ">
<ul class="dropdown-menu dropdown-menu-end " id="language-selector">
{{- if $page.IsTranslated -}}
{{- range $page.AllTranslations -}}
<li><a class="dropdown-item {{ if eq .Language.Lang $currentLang }}active{{ end }}" href="{{ .RelPermalink }}">{{ .Language.LanguageName }}</a></li>
<li><a class="dropdown-item {{ if eq .Language.Lang $currentLang }}active{{ end }}" hreflang="{{.Language.Lang}}" href="{{ .RelPermalink }}">{{ .Language.LanguageName }}</a></li>
{{- end -}}
{{- else -}}
{{- range site.Languages -}}
<li><a class="dropdown-item" href="{{ urls.JoinPath $baseURL .Lang }}">{{ default .Lang .LanguageName }}</a></li>
<li><a class="dropdown-item" href="{{ urls.JoinPath $baseURL .Lang }}" hreflang="{{.Lang}}">{{ default .Lang .LanguageName }}</a></li>
{{- end -}}
{{- end -}}
</ul>
Expand Down
10 changes: 5 additions & 5 deletions layouts/partials/footer/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
{{- $state := .state | default "async" -}}
{{- if or $header (eq $state "immediate") }}{{ $state = "" }}{{ end -}}

{{ $config := $page.Scratch.Get "modules" }}
{{ if not $config }}
{{ errorf "partial [footer/scripts.html] - Cannot initialize module configuration" }}
{{ end }}

{{- $modules := "" -}}
{{ if $core }}
{{ $config := $page.Scratch.Get "modules" }}
{{ if not $config }}
{{ errorf "partial [footer/scripts.html] - Cannot initialize module configuration" }}
{{ end }}

{{- $modules = $config.core -}}
{{- if reflect.IsSlice $config.localize -}}
{{- range $index, $mod := $modules -}}
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5dd0fce

Please sign in to comment.