Stirling-PDF/src/main/resources/static/js/languageSelection.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
3.4 KiB
JavaScript
Raw Normal View History

document.addEventListener("DOMContentLoaded", function () {
setLanguageForDropdown(".lang_dropdown-item");
2024-01-18 22:57:41 +01:00
// Detect the browser's preferred language
let browserLang = navigator.language || navigator.userLanguage;
// Convert to a format consistent with your language codes (e.g., en-GB, fr-FR)
browserLang = browserLang.replace("-", "_");
2024-01-18 22:57:41 +01:00
// Check if the dropdown contains the browser's language
const dropdownLangExists = document.querySelector(`.lang_dropdown-item[data-language-code="${browserLang}"]`);
2024-02-11 17:47:00 +01:00
// Set the default language to browser's language or 'en_GB' if not found in the dropdown
const defaultLocale = dropdownLangExists ? browserLang : "en_GB";
const storedLocale = localStorage.getItem("languageCode") || defaultLocale;
2024-02-11 17:47:00 +01:00
const dropdownItems = document.querySelectorAll(".lang_dropdown-item");
2024-02-11 17:47:00 +01:00
for (let i = 0; i < dropdownItems.length; i++) {
const item = dropdownItems[i];
item.classList.remove("active");
if (item.dataset.languageCode === storedLocale) {
item.classList.add("active");
}
item.addEventListener("click", handleDropdownItemClick);
}
2023-06-02 21:15:10 +02:00
});
2023-08-28 11:17:02 +02:00
function setLanguageForDropdown(dropdownClass) {
const defaultLocale = document.documentElement.language || "en_GB";
const storedLocale = localStorage.getItem("languageCode") || defaultLocale;
const dropdownItems = document.querySelectorAll(dropdownClass);
2023-08-28 11:17:02 +02:00
for (let i = 0; i < dropdownItems.length; i++) {
const item = dropdownItems[i];
item.classList.remove("active");
if (item.dataset.languageCode === storedLocale) {
item.classList.add("active");
2023-08-28 11:17:02 +02:00
}
item.addEventListener("click", handleDropdownItemClick);
}
2023-08-28 11:17:02 +02:00
}
2023-06-02 21:15:10 +02:00
function handleDropdownItemClick(event) {
event.preventDefault();
const languageCode = event.currentTarget.dataset.bsLanguageCode; // change this to event.currentTarget
if (languageCode) {
localStorage.setItem("languageCode", languageCode);
2023-06-02 21:15:10 +02:00
const currentUrl = window.location.href;
if (currentUrl.indexOf("?lang=") === -1) {
window.location.href = currentUrl + "?lang=" + languageCode;
2023-08-20 22:57:19 +02:00
} else {
window.location.href = currentUrl.replace(/\?lang=\w{2,}/, "?lang=" + languageCode);
2023-08-20 22:57:19 +02:00
}
} else {
console.error("Language code is not set for this item."); // for debugging
}
2023-06-02 21:15:10 +02:00
}
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll(".nav-item.dropdown").forEach((element) => {
const dropdownMenu = element.querySelector(".dropdown-menu");
if (
dropdownMenu.id !== "favoritesDropdown" &&
dropdownMenu.children.length <= 2 &&
dropdownMenu.querySelectorAll("hr.dropdown-divider").length === dropdownMenu.children.length
) {
if (
element.previousElementSibling &&
element.previousElementSibling.classList.contains("nav-item") &&
element.previousElementSibling.classList.contains("nav-item-separator")
) {
element.previousElementSibling.remove();
}
element.remove();
}
});
2023-06-02 21:15:10 +02:00
//Sort languages by alphabet
const list = Array.from(document.querySelector('.dropdown-menu[aria-labelledby="languageDropdown"]').children).filter(
(child) => child.matches("a"),
);
list
.sort(function (a, b) {
return a.textContent.toUpperCase().localeCompare(b.textContent.toUpperCase());
})
.forEach((node) => document.querySelector('.dropdown-menu[aria-labelledby="languageDropdown"]').appendChild(node));
});