Skip to content

Commit

Permalink
add event listener for language dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRDOrazio committed Sep 8, 2024
1 parent 2bb0215 commit c75711a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions assets/js/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Define the common callback function
const handleLanguageChange = (event) => {
// Retrieve the ID of the clicked item
const itemId = event.target.id;

// Extract the {key} value from the ID (assuming ID format is 'langChoice-{key}')
const key = itemId.split('langChoice-')[1];

// Set a cookie with the name 'currentLocale' and the extracted {key} value
document.cookie = `currentLocale=${key}; path=/;`;

// Reload the page
location.reload();
}

document.addEventListener('DOMContentLoaded', function() {
// Get all elements with class 'dropdown-item' that are children of the element with ID 'langChoicesDropdownItems'
const dropdownItems = document.querySelectorAll('#langChoicesDropdownItems .dropdown-item');

// Loop through each element and add a click event listener with the callback function
dropdownItems.forEach(function(item) {
item.addEventListener('click', handleLanguageChange);
});
});
1 change: 1 addition & 0 deletions layout/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<script src="https://cdn.jsdelivr.net/npm/[email protected]/i18nextHttpBackend.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/1.1.2/js/bootstrap-multiselect.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
<script src="assets/js/common.js"></script>
<script>const locale = '<?php echo $i18n->locale ?>';</script>
<?php
if ($pageName === 'admin') {
Expand Down

0 comments on commit c75711a

Please sign in to comment.