Replace subdomain for translation subdomain Using JavaScript?

Viewed 94

I want to make a translation language with subdomain, let's say my origin language is in Indonesian with www.domain.com, and i want to make the english version with domain en.domain.com, so when user click it, the page will change to english So, I'm trying to replace the subdomain name from "www.domain.com/path/.." to "en.domain.com/path/..", using JavaScript I am trying to write this code but it doesn't work

            <script type="text/javascript">
        /* <![CDATA[ */
        function doGTranslats() {location.href=location.protocol+'//'+location.hostname.replace('www.', '').replace(RegExp('^' + en + '\\.'), '')+location.pathname+location.search;}
        /* ]]> */
        </script>

<li><a href="#" onclick="doGTranslats();"hidefocus="true"><font>English</font></a></li>

Any idea how to fix this?

1 Answers
let hostParts = window.location.hostname.split('.');
hostParts[0] = 'en';
hostParts = hostParts.join('.')
Related