My goal is to dynamically redirect users based on their language choice selected from a dropdown menu, while retaining their current website location.
For example:
If the user visits xxxx.com, they should be redirected to xxxx.com/langchoice.
If the user is on xxxx.com/currentlang/test.php, they should be directed to xxxx.com/langchoice/test.php.
If the user accesses xxxx.com/test.php, they should be taken to xxxx.com/langchoice/test.php.
I have already implemented solutions for scenarios 1 and 2. However, I am not entirely satisfied with my current code, as it requires manual modification every time a new language needs to be added. Is there a more efficient way to achieve this?
var currentLocation = window.location.href;
if (currentLocation.indexOf(".php") != -1)
{
if (currentLocation.indexOf("/en/") != -1)
{
var newLocation = window.location.href.replace("/en/", "/"+evt.selectedItem+"/");
}
else if (currentLocation.indexOf("/gr/") != -1)
{
var newLocation = window.location.href.replace("/gr/", "/"+evt.selectedItem+"/");
}
else if (currentLocation.indexOf("/it/") != -1)
{
var newLocation = window.location.href.replace("/it/", "/"+evt.selectedItem+"/");
}
window.location.replace(newLocation);
}
else
{
var newLocation = window.location.href.replace("#", "") + evt.selectedItem;
window.location.replace(newLocation);
}