Change page URL on language change

Hello all,

I am not a programmer (as will become evident below) and have been struggling with building the following functionality in Corvid:

We have a blog on our page, and since multilingual is not supported in the blog module (yet?) I am trying to build a workaround. This is possible via blog categories as languages, since they are reflected in the URL. So I basically just replace the URLs based on the URL that is loading. On the main page it works stable now, though probably not elegantly coded:

$w.onReady(function () {
var u = wixLocation.url
if (u == "https://www.mainurl.com/artikel") {
	wixLocation.to("/artikel/categories/deutsch")
    }
else if (u == "https://www.mainurl.com/artikel?lang=de") {
	wixLocation.to("/artikel/categories/deutsch")
    }
else if (u == "https://www.mainurl.com/artikel/categories/english?lang=de") {
	wixLocation.to("/artikel/categories/deutsch")
    }
else if (u == "https://www.mainurl.com/artikel?lang=en") {
	wixLocation.to("/artikel/categories/english?lang=en")
    }
else if (u == "https://www.mainurl.com/artikel/categories/deutsch?lang=en") {
	wixLocation.to("/artikel/categories/english?lang=en")
    }
else if (u == "https://www.mainurl.com/artikel/categories/english") {
	wixLocation.to("/artikel/categories/english?lang=en")
    }
});

I run the same code with .onChange, since I got the impression that onReady doesnt always fire. If you can think of a leaner way of doing this, I’d be very grateful.

My main issue though is get a similar functionality on the (theoretically infinite amount of) article pages. I prepared the solution by giving the language versions the same URL, except a self-built appendix to the URL (“-plen” & “-plde”). And then running this:

$w.onReady(function () {
let language = wixWindow.multilingual.currentLanguage;
let u = wixLocation.url
if ( language == "en" ) {
 let un = $w(u).text.replace("-plde", "-plen")
    wixLocation.to(un)
    }
else if ( language == "de" ) {
 let un = $w(u).text.replace("-plen", "-plde")
    wixLocation.to(un)
    }
});

Again, I run the same code with onChange, I was looking for a way to trigger the code every time someone clicks on the languageselector, but the element doesnt seem to support onClick. This version here works, sometimes, but so far just once, and just from DE to EN.

Do you have any hints how I could make this more elegant and reliable?

Thanks a lot in advance!

Patrick

Another thing I just noticed, Google Analytics parameters in the URL mess up the functionality of my code. I could really use some help here. :frowning: