Site of Cao Shouguang, English to Chinese translator

The context sensitive Language Versions link

Most bilingual sites only have a fixed link to the other language version. Wherever you are on one language version of a site, clicking on the Language Version link always takes you to the home page of the other version of the site. But I want my visitors get the exact page they are viewing written in the other language. For example, if my visitor is visiting


by clicking “中文” at the bottom, he is taken to


So it involves replacing the “/en/” the URL of the current page with “/cn/”.

The following php code gives out wrong results:


Whichever page you are visiting, $url_old always gives the home URL “/en/index.php”. This is because I turned Permalinks on and the URL is rewritten. The URL rewritting takes place after the execution of PHP codes on the server side, and therefore the php string “$url_old” cannot get the actual URL as it is rewritten afterwards. The following java script can get the right current URL as java script runs on the client’s computer:

More information:

java replace () method

Leave a Comment