PHP Code

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&#91;'SCRIPT_NAME'&#93;;
	$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>
Share

9 Comments

  1. 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

  2. If you are unable to use the code above, you'll probably need some basic PHP lessons. Go to w3schools.com for that

  3. 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

  4. yes, use the code above!

    It requires manual translation of each page though

  5. i want to know how create multiple language using php

    thanks in advance
    ganesh

  6. 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.

  7. 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.

  8. 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.

  9. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image