0

I have a website with three different languages place in separate folders as below.How am I able to change the en to cn if I click on the change cn language button?

www.example.com/en/content

www.example.com/cn/content

www.example.com/bn/content

<div class="language">
   <a href="#/" id="EN" data-language="en">EN</a>
   <a href="#/" id="CN" data-language="cn">CN</a>
   <a href="#/" id="BN" data-language="bn">BN</a>
</div>

jquery

$(function changelang() {
    $(".language a").click(function(e) {
        var selectedLanguage = $(this).attr("data-language");

        var currentContent = location.pathname.substring(
            location.pathname.indexOf("/", 1) + 1,
            location.pathname.length
        );

        // console.log(selectedLanguage, " >> ", currentContent);
        window.location.href = "/" + selectedLanguage + "/" + currentContent;
    });
});
1
  • 2
    so what was the issue...? Commented Apr 3, 2018 at 13:38

1 Answer 1

1

The basic problem is that you need to cancel the default link action

so add e.preventDefault() in your click handler.

$(function changelang() {
    $(".language a").click(function(e) {
        e.preventDefault();

        var selectedLanguage = $(this).attr("data-language");

        var currentContent = location.pathname.substring(
            location.pathname.indexOf("/", 1) + 1,
            location.pathname.length
        );

        // console.log(selectedLanguage, " >> ", currentContent);
        window.location.href = "/" + selectedLanguage + "/" + currentContent;
    });
});

And here are two alternate ways to do the same thing (because i had misunderstood the question initially)

Regular expression

$(function changelang() {
    $(".language a").click(function(e) {
        e.preventDefault();
        var selectedLanguage = $(this).attr("data-language");

        window.location.href = window.location.pathname.replace(
          /\/.+\/(.+)$,
          `/${selectedLanguage}/$1`
        );

    });
});

Split, modify, rebuild

$(function changelang() {
    $(".language a").click(function(e) {
        e.preventDefault();
        var selectedLanguage = $(this).attr("data-language");

        var currentContent = location.pathname.split('/')
        currentContent[1] = selectedLanguage;

        // console.log(selectedLanguage, " >> ", currentContent);
        window.location.href = currentContent.join('/');
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much the code is working. But may I know what does it mean currentContent[1] ?
@user9030616 the split method of a string returns an array of the parts separated by the parameter you pass, in this case the /. The [1] targets the element with index 1 in that array, in this case the language, and replaces it with the new lang. Then we join the array to a string to get the new url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.