Ive made a webside with 3 languages with blocs, but this one uses a lot of pages, and count of it rising every week.
The main goal is simple button click to change language and stay on same page without integration of http link to every page.
Maybe it’s possible to change link with java?
For example:
proposals-de.html
Can be change by some code with this function:
If webpage name have “-de” and button with “-en” was clicked, than find “-de” and replace with “-en” and follow new link!
There is probably a better way to code this, I’m still learning Javascript. (pretty sure there is a way to easily replace everything after the - that’s better than what I am doing here) But it works.
Step 1
Give each language button a class called
.btn-lang
And add a custom attribute called (replace the value for the language)
data-language = "fr"
Step 2
Post this code into the footer of the page.
<script>
var currentURL = window.location.href;
var sliceURL = currentURL.substring(0, currentURL.length - 7);
document.querySelectorAll('.btn-lang').forEach(function(button) {
var newLang = (button.getAttribute("data-language"));
(button.setAttribute("href", sliceURL + newLang + ".html" ));
});
</script>
So the trick is that this code read currentUrl than create sliceUrl by deleting 7 characters and after takes attribute from button and add to SliceUrl?
And button should be with empty HREF?
And the script should be placed at the head of page?
If I change button to flag image I should use IMG instead of BUTTON?
I tested it as a button, not an image. (Adding those sort of details in your original post can help) Let me get back to you. You can see the “a“ anchor is wrapping the image, so of course it’s not going to use the href that was created attached to the image itself.
Ok I made some modifications, the code will add an “Onclick” to the image. So you don’t need to use the interaction option to make the image a link. And it will also turn the curser into a pointer.