Toggle web site language easily with PHP
Many new content management systems have inbuilt mechanisms for managing and toggling between content in multiple languages. Unfortunately, a CMS may be serious overkill for most smaller sites, yet they still need to serve their few pages in more than one language. Here's a simple way to allow for this.
I make it a general rule to make separate files for my header (including navigation) and footer files, and simply include them on all pages within my site. That way if I need to make a change, I make it in one place, such as header.php. This method also makes it very easy to provide a link to alternate languages.
How it works
So let's say you want to provide content in English and Spanish. You have the following pages:
1. index.php
2. about.php
3. contact.php
All 3 of them include header.php and footer.php. All you need to do is create a copy of each file, with the language short form as an extension. (index_es.php, about_es.php, etc). Same with the header/footer files.
Now, at the top of the English header, we'll place the language option, providing a link to the same page in English or Spanish. The beauty of this is that we don't have to program the link on each page, we can use PHP to dynamically determine it at run-time. Here's how:
<?php
global $title;
$this_page = $_SERVER['SCRIPT_NAME'];
$en_ext = ".php"; // the extension for English pages
$es_ext = "_es.php"; // the extension for Spanish pages
$new_link = str_replace($en_ext, $es_ext, $this_page); //reverse this to go back to English from a Spanish page
?>
Now here's the code to output the link. I included the logic to not provide a Spanish link in the support section, because support is a web app that's only in English.
<div id="languages">
English
<?php if(!is_string(strstr($this_page, "/support/"))) { ?>
| <a href="<?php echo($new_link); ?>">Italian</a>
<?php } ?>
</div>


on June 25th, 2007 at 9:37 am
hello,
I want to impliment the multi language support in application ,so how can i do it by the code listed above ?
pls help me …it's urgent..
thanks
krut
on June 25th, 2007 at 9:41 am
If you are unable to use the code above, you'll probably need some basic PHP lessons. Go to w3schools.com for that
on June 26th, 2007 at 1:51 am
hi,
thanks for advice and quick response,but for that it's need to create differrent pages for different languages.Is there any way to have a single page in english and when user presses the link e.g. spanish then the page will be displayed in spanish.
thanks
krut
on June 26th, 2007 at 8:53 am
yes, use the code above!
It requires manual translation of each page though
on January 6th, 2008 at 11:58 pm
i want to know how create multiple language using php
thanks in advance
ganesh
on August 12th, 2008 at 8:15 am
I use ebay to sell products online. I wanted to start selling internationaly and figured if i had multi languages on my ebay page i would get more business. Is there anyway I can somehow create a link that lets my buyers translate the page in whatever language is helpful for them to understand the contents of the page. If this is possible, whatever way is the easiest please tell me.
on October 14th, 2008 at 3:14 pm
The proper way to add internationalization to your PHP code is through the use of Gettext. It's better to start things off the right way than incorporate a hack and have to go back and fix it later.
on May 8th, 2009 at 10:38 am
Php can automate link generation, but such approach would need a lot of files (if you do not have modrewrite). You;ll have to create files for all the pages if you decide to add one more language one day.
IMHO much more flexible approach would be storing selected site language in a cookie, and use custom function to retrieve text in specified language from some array.
on February 2nd, 2010 at 7:46 pm
I don't really understand how you are doing this. A sample would help but I think that I prefer to do it a different way but I just don't know how to do it.
I do like this but I prefer to do this a different way and I think it keeps files more organized but I'm not sure how to do it. Perhaps someone can tell me.
I want to have each language files in their own folder (en/ and fr/) for english and french for example. I want to be able to click on the image/text and have the page I'm currently on change to the same page but in a different language. So essentially, we are just changing the folder name from en to fr.
Can anyone please tell me how to do this? A sample site here but it's in ASP code (http://www.contacservices.com/)
Thank you in advance.