summaryrefslogtreecommitdiffstats
path: root/themes/danix-xyz-hacker/assets/js/language-switcher.js
blob: 3d8ffac42e1c54801950b16051a7f34b0b481a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
document.addEventListener('DOMContentLoaded', function() {
  // Get all language switcher elements
  const langSwitchers = document.querySelectorAll('[data-lang-switch]');

  if (!langSwitchers || langSwitchers.length === 0) {
    return;
  }

  // Add click event listener to each language switcher
  langSwitchers.forEach(function(switcher) {
    switcher.addEventListener('click', function(e) {
      e.preventDefault();

      // Get the language code from the data attribute
      const langCode = this.getAttribute('data-lang-switch');

      // Store the language preference in localStorage
      localStorage.setItem('preferred-language', langCode);

      // Navigate to the language-specific URL
      window.location.href = this.href;
    });
  });
});