The site has several localizations: ru and en.

How to do so - if the user is on the page http://site.com/sample.html so that a link could be displayed on this page to another language of this page http://site.com/en/sample.html And this link changed to depending on the page on which the user is located.

    2 answers 2

    The answer is corrected after clarifying the question.

    Add to all pages:

    <a href='#' id='download'></a> <script type="text/javascript"> window.onload=function(){ var a = document.getElementById('download'); var path = window.location.pathname; var host = window.location.host; if (path.substring(0,4) == '/en/'){ newpath = host + path.substring(3); var val = 'Рус'; } else { newpath = host + '/en' + path; var val = 'En'; } a.href = newpath; a.text = val; } </script> 
    • Thank you so much for the answer, but not at all :). There are about 3 thousand pages on the site. The link should change dynamically depending on the page. Most likely it makes sense to define the page URL, change it and only then output it with the stand "/ en /" or remove this "en" from the display URL. - Andrei
    • Then you should clarify your question :) - Samuel Loog
    • Thought to make through document.location.href but something stalled. Can there be ideas for implementation? - Andrei
    • Then, alternatively, js: window.location.host will give you site.com , and window.location.pathname will give you /path/to/sample.html . take the first + / en + second - we get the desired address. If the second one starts with / en /, then, on the contrary, we cut off and get the right link again - Samuel Loog
    • On the page when generating in the right place, <a href='#' id='lang'></a> link <a href='#' id='lang'></a> , then when the js page loads, it receives its address, by id receives the link element, parses the page address, inserts the link (this If you do it through js, you can still do it from the server side when generating the page, but it depends on what you are using.) - Samuel Loog

    Slightly corrected the answer Samuel Loog and it turned out. Worked version checked. Thank you, I could not have been without you.

     <script type="text/javascript"> window.onload=function(){ var a = document.getElementById('download'); var path = window.location.pathname; var host = window.location.host; if (path.substring(0,4) == '/ua/'){ newpath = path.substring(3); var val = 'Рус'; } else { newpath = '/ua' + path; var val = 'Укр'; } a.href = newpath; a.text = val; } </script> 
    • and how does your answer differ from the answer @SamuelLong besides, how are you /ua instead of /en ?) - Denis
    • Please pay attention to the formation of links, lines 7 and 10. - Andrew
    • The problem is most likely that I wrote the answer with absolute absolute references, and in your case, I suppose, relative ones are used. In the question about this not a word, besides, an example will result with absolute references. - Samuel Loog
    • I do not in any way criticize your answer, without your answer nothing would have happened, thank you so much. He was just what he needed, just a little tweak to correctly form the link. - Andrei
    • 3
      @ Andrei, it’s customary to tick the answer that helped you (which became the basis for your decision) as a thank you to the respondent. - Nofate