View Full Version : Switching language pages in PHP - help, please

10-02-2009, 01:11 PM
Hi, everyone,

I've been searching for this for a while now but am coming up dry. I'm looking for a language switch script that allows the following:

If a user is on http://www.mysite.com/en/page.php and clicks the 'French' link in the nav menu, it switches them automatically to the French version of that specific page: http://www.mysite.com/fr/page.php (and vice versa).

Any assistance would be very much appreciated!

Cheers. ;)

10-03-2009, 02:49 AM
This is a complex question.

There are a few parts:

1. You need to translate all the pages.

2. You need to have a way to transfer people (and save the language setting, if you want).

3. You need to have copies of all the pages in each language.

1. Translation:
This is ALWAYS best done by a person, not a machine. The easiest way to deal with this is to recode your pages so they work with sections/phrases and translate each section individually, like "hello" is stored somewhere as "bonjour" so you can replace it later.
If you can translate it yourself, great. If not, see if you can find some help. If you have a big enough/popular site, then you can get visitors who speak a language to help translate it. That's how the big sites do it (like facebook, for example).
The alternate method here is to use a machine translator and then save the content (like google translate), which is always a bad idea because there will ALWAYS be errors, and might actually be harder to read than just making the visitor view it in English-- after all they can always cut/paste the page into a translator themselves... that doesn't help much.
You could also look for a setup for your server that will automatically translate (like running a copy of google translate on your server, if that code was available), but it will be hard to find a reasonable translator, at least for free. And again, it won't turn out that well.

2. Language choice/links:
There are many ways to go about this. The easiest way is to store a variable somewhere (cookie, database, session data), and use that to load the pages. You don't even need /fr/ or /en/, because you could just use the general page, but program it to present the content in the language matching the choice. So you need links somewhere on the page(s) that allow a language change, then just store that variable once the links are clicked, and finally display content based on that variable.

3. Site structure:
You will need to recode your entire site to make this work.
There are a couple approaches.
a) You can just create a whole copy of your site (like /en/ and /fr/) manually and update it manually. This is easiest to set up, but hardest to maintain. It's not a very good idea unless your site never changes.
b) You can use a single page setup and load phrases like I mentioned before. You can use files on your server, or better a database. You would then make a function (for example) that works like this language('Hello') which would of course take that input and "translate" it based on a list you have somewhere. This is how slightly more complex systems work. Remember you can leave the default (if the phrase is not defined) as just keeping the English, so you can always have SOME content, even if it isn't actually translated. This means you can keep the website running in French, even if it takes a long time to actually get all the translations, or if the content changes too quickly to keep up.
c) Finally, if you want to make the effort to do it the best (but most complex) way, then you can use a full CMS system to generate ALL of the content on your site, including switching for language. This means you would use something like joomla to control all of the content and use a template/content two-part setup for your whole site.

I hope this gives you an idea where to start. You need to now decide what is the best for your site. Generally pick (a) for small sites, (b) for bigger projects, and (c) for complex large sites with a lot of content that is often changing.

10-03-2009, 01:40 PM
Thanks for the thorough reply! That was more than I expected.

All of the pages are translated and there won't be a whole lot of maintenance so I decided to have two directories: en/ and fr/ each with the same structure just with the translated copy so I'm not concerned about scope or maintenance challenges.

I suppose I could hard code the URLs to switch from, say en/about/index.asp to fr/about/index.asp but I'd rather use a dynamic solution in the URL in the event that the site does become a little less simplified in the future.

I stumbled across this which I named language-switch.php and saved to my root dir:

$completeNewURL = "";

$referer = "$HTTP_REFERER ";
$brokenDownReferer = "";

if (stristr($referer, "/en/")){
$brokenDownReferer = str_replace("/en/", "/fr/", $referer);
$brokenDownReferer = str_replace("/fr/", "/en/", $referer);

//These two lines of code will redirect the user to the corresponding
//french/english version of the page they were just on.

header ("Location: $brokenDownReferer");

and then in my en/about/index.php file I hyperlinked this:

<a href="../includes/language-switch.php">Francais</a>

After confirming that the path to the language-switch.php file was correct the script didn't work. Unfortunately, I'm running this test on localhost so I can't post an example.

Cheers ;)

10-03-2009, 04:56 PM
we can change the language through the google api ? is that possible ? or any other website provide online translate our web ?

10-03-2009, 05:20 PM
we can change the language through the google api ? is that possible ? or any other website provide online translate our web ?

Probably, but those translations aren't very precise in many cases.

10-03-2009, 06:06 PM
Probably, but those translations aren't very precise in many cases.

Exactly. When it comes to the actual translation of web content, you're better off hiring someone to do the translating. That's why I'm just looking for a script to swap the directory since my content is already translated.

10-03-2009, 08:59 PM
The approach in the script above looks workable enough.

If ALL you need is the link, then it's not very complex at all.

Simply have an a tag like this:
<a href=".....">Switch Language</a>

(If you want "Language" to say the language, then you need to store a variable for the current setting and use the opposite one, OR you can extract it from the URL-- ie, "if (fr), echo 'en'), etc.)

Anyway, for the href:

$currenturl = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
echo strpos($currenturl,'/en/')?str_replace('/en/','/fr/',$currenturl):str_replace('/fr/','/en/',$currenturl);
( condition?true:false; )

Something like that should work.

If you end up with more than two languages, it's better to store a variable and have a menu for choosing the new language.

I think that string (HTTP_HOST.REQUEST_URI) should work, but I'm not positive that will always be the full URL. If that doesn't work, google for a better "current url in php" or just ask again. From a quick google search that looks right, but it might not be entirely reliable, so just a warning there.

10-04-2009, 12:37 AM
Ah, success! It turns out that the REQUEST_URI string was all that was needed else it returns the URL twice. Here's the modified code:

$currenturl = $_SERVER["REQUEST_URI"];
echo strpos($currenturl,'/en/')?str_replace('/en/','/fr/',$currenturl):str_replace('/fr/','/en/',$currenturl);

I tested it in a couple of different pages within each en/ and fr/ dir to make sure it was switching properly and it works like a charm.

I can't thank you enough for the quick response and assistance. You are one class act, sir.

Cheers ;)

10-04-2009, 01:39 AM
The issue with REQUEST_URI is that I'm not sure exactly what the format is.

For any given page, you can have many links to it:
Assuming you are on http://example.com/dir/1.php
<a href="http://example.com/dir/2.php">
<a href="/dir/2.php">
<a href="2.php">

Any of those will take you to the same page, so depending on how your links are set up (absolute, relative, or something in between), then you may not get the /fr/ part of the url in it every time. I think that the browser will format any of those with the relative location it's already at-- so you end up with a reasonable link. But I'm not sure about the best way to test this, because it would require setting up many pages and testing for all possibilities.

However, if it's working that's great, and no reason to mess with it. But if you run into problems it is likely due to that.