Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding Language Selector to my website but facing a problem

I am building a website that has English and Macedonian but the problem is, when I click in "MK" it changes to the Macedonian version of my website but the selector stays on EN and if I wanted to switch back to English I couldn’t the selector becomes useless. What is the problem that I can’t find? Here is the code for html and js.

`<header>
    <nav>
      <center>
        <ul class="nav-links">
          <li><a href="/">Home</a></li>
          <li><a href="contact.html">Contact</a></li>
          <li><a href="about.html">About</a></li>
          <li>
            <select id="language-selector">
              <option value="en">EN</option>
              <option value="mk">MK</option>
            </select>
          </li>
        </ul>
      </center>
    </nav>
  </header>



  <script>
    const langSelector = document.querySelector('#language-selector');
    
    langSelector.addEventListener('change', function() {
      let lang = this.value;
      
      let url = window.location.href;
      let newUrl;
      
      if (lang === 'mk') {
        newUrl = url.replace(/(index.html)$/, 'mk/home-mk.html');
      } else {
        newUrl = url.replace(/(mk\/home-mk.html)$/, 'index.html');
      }
      
      window.location.href = newUrl;
    });
  </script>`

I tried everything that came to my mind I even changed the whole js but I can’t seem to figure it out.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add the selected attribute for the MK option on the mk/home-mk.html page.

<select id="language-selector">
    <option value="en">EN</option>
    <option value="mk" selected>MK</option>
</select>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading