4

I have tried to look for answers here and on google, but maybe I'm not using the correct search terms... I just can't find it. Hope anyone can help me with this.

On a bi-lingual shop I want a person to be redirected to the same product on a different domain/url, when clicking the english flag button.

www.mydomain**.nl/a1**/product1.html

is the starting point. Clicking on the english flag should grab the domain and category and change this and go to the newly created url:

www.mydomain**.com/a2**/product1.html

then

www.mydomain**.nl/b1**/product1.html ,becomes after clicking the 'english flag' button:

www.mydomain**.com/b2**/product1.html

etc.

There are about 30 different pages to be redicted like this, all with the same 'english flag' button.

IF part of url is '.nl/schoenen/' THEN change and go to '.com/shoes/'

IF part of url is '.nl/jassen/' THEN change and go to '.com/jackets/'

Richard
  • 91
  • 8

4 Answers4

1

TL;DR - Use some javascript to manipulate the url onclick, reassemble the URL the way you wish, reload new URL.

I did a bilingual shop once and inserted an 'es' or an 'en' into the path to signify Spanish or English, essentially as a URL parameter. Your goal is a bit more complicated (I'll assume it has to be) but this might get you started. You can see the shop here (not my design -- don't blame me. Much of it is offline at the owner's request (some legal stuff) but you should be able to navigate some pages and see the language toggle in effect.)

Clicking a language toggle triggers some simple Javascript as follows:

function switchLanguage(lang) {
  u = location.href.split('/');
  u[3] = lang;
  location.href = u.join('/');
}

And then an onclick is added to the language toggle links to direct onclick="switchLanguage('en')" or onclick="switchLanguage('es')", or whatever your URL injection is to be. Again, this is specific to my example where the URL parameter was going to follow the third forward slash (u[3] = lang), but this should get you started on dicing up your URL for manipulation and reassembly.

There are some excellent examples here.

Of course, the localized versions of my site needed to reside under those url paths (in my case, I changed some PHP global variables to account for the different language codes in the URL, so the under-the-hood stuff is really minimal) but this is one method to get you started on how to split/alter/replace your URL onclick.

Community
  • 1
  • 1
dashard
  • 877
  • 1
  • 10
  • 17
0

Simply use the IF statement, if the button is clicked, the URLs will be changed! Each url leads to a page in different language. If I got you right

Jad
  • 479
  • 2
  • 10
  • 23
  • 1
    IF part of url is '.nl/schoenen/' THEN change and go to '.com/shoes/' IF part of url is '.nl/jassen/' THEN change and go to '.com/jackets/' i hope this is more clear? Thanks! – Richard Jul 23 '14 at 12:52
  • this is very different meaning from what I understood. I suggest you to add this comment to the main question, as it isn't clear enough! – Jad Jul 23 '14 at 12:55
0

The Url you are curently on is in the window.location object. When you want to redirect, retrieve it (window.location.href for example), change the domain using string operations and redirect the window ?

Sorry if I havent understood your problem correctly.

astian
  • 684
  • 7
  • 18
0

You could create a method which "translates" the current url:

function translateUrl(string transArticleName) {
    var newTerm = ".com/"+transArticleName+"/";
    return window.location.href.replace("/.nl/[\w]{1,}//", newTerm);
} 

Or you read the article name and translate it to english:

function translateUrl(string transArticleName) {
    var oldTerm = window.location.href.match("/.nl/[\w]{1,}//")[0];
    var aName = oldTerm.match(/\/[\w]{1,}\//)[0].match(/[^\/]{1,}/)[0]
    var transArticleName = //translate aName server side
    var newTerm = ".com/"+transArticleName+"/";
    return window.location.href.replace(oldTerm, newTerm);
} 

As you can see I'm not the master of regex' so I'm sure there is a nicer way to use them here.

Florian Gl
  • 5,984
  • 2
  • 17
  • 30