-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1091 from d-oit/main
Save and load the selected language on domain level
- Loading branch information
Showing
10 changed files
with
85 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/_vendor | ||
_vendor/ | ||
exampleSite/public/ | ||
public/ | ||
resources/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.