I want to create a little menu to change the language of a static site. There are two languages, German which is the main language placed in the base folder of my project and english. I put the english version into a subfolder /en. Now I want to be able to switch between the languages without hardcoding the absolute urls into each htlm file, which is why I wrote some JS. But I think it's only a dirty solution:
The urls for the two index files for example are like so:
German: http://www.somedomain.de/project/index.html
English: http://www.somedomain.de/project/en/index.html
The html to select a language, simplified:
<h3>Please select your language</h3>
<button class="deutsch">Deutsch</button>
<button class="english">English</button>
And here's how I manipulate the url on click:
var path = window.location.href;
var filename = path.replace(/^.*[\\\/]/, '');
var base = path.split("ct")[0]; //"ct" is simply taken from the end of the path "/project/"
$('.english').click(function(){
window.location.href = base + 'ct/en/' + filename; // For English
});
$('.deutsch').click(function(){
window.location.href = base + 'ct/' + filename; // For German
});
The ugly part is within the "var base". I needed to copy the url that comes before /filename.html and I had no other idea than to split the whole path and than put everything together again with or without the language directory. It works fine but to me that looks really cumbersome.
Can someone suggest a nicer method to accomplish the same result?