I'm trying to create a language switcher to change between two languages Nederlands/English
I need the link to be changed whenever I'm on a page, like this
www.example.nl/page/lorem to www.example.nl/en/page/lorem
www.example.nl/en/page/lorem to www.example.nl/page/lorem
NOT
www.example.nl/page/lorem to www.example.nl/en/
www.example.nl/en/page/lorem to www.example.nl/
This is the code I'm using, but it doesn't work:
$(document).ready(function() {
var winLocation = window.location;
var loc = winLocation + "";
if(loc.indexOf("/manasik_nl/en") != -1) {
$(".lang").prop("href", loc.replace("/manasik_nl/en", "/manasik_nl/"));
$(".lang").text("Nederlands");
}
else if(loc.indexOf("/manasik_nl/") != -1) {
$(".lang").prop("href", loc.replace("/manasik_nl/", "/manasik_nl/en"));
$(".lang").text("English");
}
});
<div class="language-switcher">
<a class="lang" href="/manasik_nl/" language="English"><img class="alignnone size-full wp-image-110" src="https://manasik.nl/manasik_nl/wp-content/uploads/2022/03/nl.png" alt="" width="18" height="12" /></a>
<a class="lang" href="/manasik_nl/en/" language="Nederlands"><img class="alignnone size-full wp-image-109" src="https://manasik.nl/manasik_nl/wp-content/uploads/2022/03/en.png" alt="" width="18" height="12" /></a></div>
I would appreciate it if anyone could help, thanks.
Just run this html code example:
<select id="languages">
<option id='1' value="English" placeholder='English'>
<option id='2' value="Nederlands" placeholder='Nederlands'>
</select>
And add some JavaScript onclick function that switch from a page to another one with window.location.
document.getElementByID('1').onclick = function(){
window.location.assign("your URL")
};