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.

80 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-06-02 21:15:10 +02:00
document.addEventListener('DOMContentLoaded', function() {
2023-08-28 11:17:02 +02:00
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('-', '_');
// 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
2024-01-18 22:57:41 +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
2023-06-02 21:15:10 +02:00
const dropdownItems = document.querySelectorAll('.lang_dropdown-item');
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-08-28 11:17:02 +02:00
function setLanguageForDropdown(dropdownClass) {
const defaultLocale = document.documentElement.lang || 'en_GB';
const storedLocale = localStorage.getItem('languageCode') || defaultLocale;
const dropdownItems = document.querySelectorAll(dropdownClass);
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
function handleDropdownItemClick(event) {
2023-08-20 22:57:19 +02:00
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
2023-08-20 22:57:19 +02:00
const currentUrl = window.location.href;
if (currentUrl.indexOf('?lang=') === -1) {
window.location.href = currentUrl + '?lang=' + languageCode;
} else {
window.location.href = currentUrl.replace(/\?lang=\w{2,}/, '?lang=' + languageCode);
}
} else {
console.error("Language code is not set for this item."); // for debugging
}
2023-06-02 21:15:10 +02:00
}
2023-08-20 22:57:19 +02:00
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('.nav-item.dropdown').forEach((element) => {
const dropdownMenu = element.querySelector(".dropdown-menu");
2023-08-23 00:03:21 +02:00
if (dropdownMenu.id !== 'favoritesDropdown' && dropdownMenu.children.length <= 2 && dropdownMenu.querySelectorAll("hr.dropdown-divider").length === dropdownMenu.children.length) {
2023-08-20 22:57:19 +02:00
if (element.previousElementSibling && element.previousElementSibling.classList.contains('nav-item') && element.previousElementSibling.classList.contains('nav-item-separator')) {
element.previousElementSibling.remove();
}
element.remove();
}
});
2024-02-11 17:47:00 +01:00
2023-06-02 21:15:10 +02:00
//Sort languages by alphabet
2023-08-20 22:57:19 +02:00
const list = Array.from(document.querySelector('.dropdown-menu[aria-labelledby="languageDropdown"]').children).filter(child => child.matches('a'));
2023-06-02 21:15:10 +02:00
list.sort(function(a, b) {
2024-01-05 19:00:30 +01:00
return a.textContent.toUpperCase().localeCompare(b.textContent.toUpperCase());
2023-08-20 22:57:19 +02:00
}).forEach(node => document.querySelector('.dropdown-menu[aria-labelledby="languageDropdown"]').appendChild(node));
2023-06-02 21:15:10 +02:00
});