How to Add Multi-language Support to a PHP Website

Posted on July 23, 2009, under PHP,  Bookmark it

Are you interested in having a multilingual website? This is a tutorial that shows you how you can do that in PHP.

php-multi-language-site

The first thing we need to do is to create a couple of files that will contain the text for each of the languages that will be supported by the website. For demo purpose I have chosen English, Spanish and German. Make a directory named “directory”. In this directory create 3 files: lang.de.php, lang.en.php, and lang.es.php. In our main file (index.php) we will include a file (common.php) containing a piece of code that gets the requested language. Here’s the content of those 3 language files:

lang.en.php

<?php
/*
------------------
Language: English
------------------
*/

$lang = array();

$lang['PAGE_TITLE'] = 'My website page title';
$lang['HEADER_TITLE'] = 'My website header title';
$lang['SITE_NAME'] = 'My Website';
$lang['SLOGAN'] = 'My slogan here';
$lang['HEADING'] = 'Heading';

// Menu

$lang['MENU_HOME'] = 'Home';
$lang['MENU_ABOUT_US'] = 'About Us';
$lang['MENU_OUR_PRODUCTS'] = 'Our products';
$lang['MENU_CONTACT_US'] = 'Contact Us';
$lang['MENU_ADVERTISE'] = 'Advertise';
$lang['MENU_SITE_MAP'] = 'Site Map';
?>

lang.es.php

<?php
/*
-----------------
Language: Spanish
-----------------
*/

$lang = array();

$lang['PAGE_TITLE'] = 'Título de la página de mi sitio web';
$lang['HEADER_TITLE'] = 'Mi sitio web de la cabecera título';
$lang['SITE_NAME'] = 'Mi Sitio Web';
$lang['SLOGAN'] = 'Mi lema aquí';
$lang['HEADING'] = 'Título';

// Menu

$lang['MENU_HOME'] = 'Inicio';
$lang['MENU_ABOUT_US'] = 'Sobre Nosotros';
$lang['MENU_OUR_PRODUCTS'] = 'Nuestros productos';
$lang['MENU_CONTACT_US'] = 'Contáctenos';
$lang['MENU_ADVERTISE'] = 'Publicidad';
$lang['MENU_SITE_MAP'] = 'Mapa del Sitio';
?>

lang.de.php

<?php
/*
-----------------
Language: German
-----------------
*/

$lang = array();

$lang['PAGE_TITLE'] = 'Meine Webseite Titel';
$lang['HEADER_TITLE'] = 'Meine Website-Header Titel';
$lang['SITE_NAME'] = 'Meine Website';
$lang['SLOGAN'] = 'Mein Slogan hier';
$lang['HEADING'] = 'Position';

// Menu

$lang['MENU_HOME'] = 'Heim';
$lang['MENU_ABOUT_US'] = 'Über uns';
$lang['MENU_OUR_PRODUCTS'] = 'Unsere Produkte';
$lang['MENU_CONTACT_US'] = 'Kontaktieren Sie uns';
$lang['MENU_ADVERTISE'] = 'Werben';
$lang['MENU_SITE_MAP'] = 'Site Karte';
?>

As you can notice, some constants are created using the define() function. In every file the defined constants have the same name, bu the values is different. We will output the values of the constants inside the index.php file. Therefore we will see different text every time we will call other language file.

Determine the right language

Let’s analyze the common.php file:

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie('lang', $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

  case 'de':
  $lang_file = 'lang.de.php';
  break;

  case 'es':
  $lang_file = 'lang.es.php';
  break;

  default:
  $lang_file = 'lang.en.php';

}

include_once 'languages/'.$lang_file;
?>

After we determine the value of $lang, we use switch() to compare its value with some different values, and execute a different piece of code depending on which value it equals to. After the value of the $lang_file is determined, the script will include the necessary language file. As you can see I have used sessions to register the value of $lang. This way users can navigate through the whole site and see the content in the chosen language (lang=[language here] does not need to be passed in every URL). Additionally, I have used cookies to store the selected language in users computer for 30 days. When the visitor will come back he will see the site in the language that he previously selected.

How if the website’s language requested?

In this demo I have chosen to use some image flags, each image having a link to index.php?lang=[LANG HERE]. So, to see the site in german we will use the German image flag which links to index.php?lang=de.

Lastly, the constants values should be outputted in the page. Examples:

for document title

<title><?php echo $lang['PAGE_TITLE']; ?></title>

for header menu

 <ul>
      <li><a href="/"><?php echo $lang['MENU_HOME']; ?></a></li>
      <li><a href="about_us"><?php echo $lang['MENU_ABOUT_US']; ?></a></li>
	  <li><a href="our_products"><?php echo $lang['MENU_OUR_PRODUCTS']; ?></a></li>
      <li><a href="contact_us"><?php echo $lang['MENU_CONTACT_US']; ?></a></li>
      <li><a href="advertise"><?php echo $lang['MENU_ADVERTISE']; ?></a></li>
      <li><a href="sitemap"><?php echo $lang['MENU_SITE_MAP']; ?></a></li>
    </ul>

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

83 Replies to "How to Add Multi-language Support to a PHP Website"

  1. This may works out for a couple of pages, but not for much, ( except the menu and static stuff ) as the page title is vital for SEO, and every page having the same title will really be a problem.

    Furthermore you’d need to write all your content in 3 languages and associate that with the pages that should display that content later on. Better is to have a nice – maybe written by your own – CMS that really does nothing else than “spit” out requested content in the requested language on the appropriate page.

    We use a small CMS for our news section, which supports 3 languages. Using such a thing together with your approach then makes sense. It displays contents on our different language websites accordingly.

  2. I wonder could that be changed so when changing the language, the hrefs of the header change aswell as the title of the link? that way i could simply just add

    “href=”index.php?page=etusivu&lang=th”

    and with include, bring in different texts to the body area, according to the language:

    include ($lang.”/etusivu.html”);

    That way, I dont need to define different words for different languages, since all words will be in separate .php or .html file.

    1. It’s good to have a certain order of the files and keep each language words in separate files. I have seen many professional applications built this way and I really recommend this practice. I don’t like the idea of changing the links inside the page. I believe it’s better to use specific links or (flag) images to set the desired language.

      1. @Gabriel,

        I aggree on the point: of using specific links or imgs to set the language, but the href that i want to change is the menu’s links “home, links, contact, etc” :) could that be possible with your script? Im a newbie sorta with php.

        Thanks :)

  3. [...] This post was Twitted by planetphp [...]

  4. Hi,

    Using constants is just the simplest way of localizing your content. Personally I got realy fond of the way WordPress has incorporated localisation. Somewhat simplified but everywhere you want to use a localized string you just wrap it in __() or _e():
    to echo a localized string. The function _e($str) knows if a translated version of the string is present in your translation set. If so the translation is outputted, otherwise the unaltered text. This makes sure you never get ugly constant warnings in your interface.
    Using any of the dozens of ways you can easily hook your __() and _e() implementation to your prefered dictionairy method of choice. Be it ini files, or xml files, or when you are up to it going with gettext (.po/.me and .pot files, with POEdit).
    This post is okee for just a simple site, but when you get into larger sites, you need to get a more sound and less error prone approach.

    My 2cts

    Arnoud

  5. Thanks :) man.
    .-= simi.ro’s last blog ..U Tv =-.

  6. Hi buddy this is very simple way to convert language……..

  7. Great tutorial. It’s what the feature I am looking for for my dating website.

    Can you tell us a bit further how do replace the flag icon with the dropdown box option.

  8. Hi

    Is it actually possible instead of redirecting each language link to the index

    Instead of <a href="?lang=en" rel="nofollow">

    use

    lt;a href="<?php $_SERVER['PHP_SELF']?>" rel="nofollow">?lang=nl">

    1. Hi Peter i think a better explaination would be better coz the second link is sensless

  9. How to modify for use with mysql database?

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

    1. hey like your site loos great – looking to do something similar with ours but possibly using sessions and cookies so you would not have to duplicate pages i.e. index_en && index_fr etc..

  11. Very usefull tutorial!
    I have a doubt. How can I add to this code a php function to identify the default browser language?

  12. Can anyone tell me how to use multilanguage for a dynamic content from database

  13. thank for your very good posting

  14. Hi @ e11world

    That’s fairly easy. Create a new directory/folder and named it to english for english and spanish for spanish and german for german and so on.

    After that, create lang.en.php, lang.es.php and lang.de.php. Rename these files like lang.en.php into english.php and so on and put them in their respective directories.

    Next step, you’ll have to modify the common.php.

    Look for line 29 $lang_file = ‘lang.en.php’; and replace with this code below:

    $lang_file = ‘english’; //let’s remove the .php extension instead^^

    Do this for the other languages you like.

    Now, go into line 45 and change this code: include_once ‘languages/’.$lang_file;

    into this code below:

    include_once ‘languages/’.$lang_file.’/’.$lang_file.’.php’;

    You now have languages in each directories. It’s more clean and cumpty this way specially when you suddenly decided to separate each language section in each files, you’re translator won’t have a hard way.
    To change language, use the below code.

    @ket

    To replace it with drop down menu, you’ll have to use this example code below:

    function jump(menu){
    var loc = menu[menu.selectedIndex].value.split(“|”);
    if(loc.length == 2)
    window.open(loc[1], loc[0]);
    }

    <option value="_top|?&lang=en” >English
    <option value="_top|?&lang=de” >German
    <option value="_top|?&lang=es” >Spanish

    It will repeat the requested language over and over until you click other links. I know, the javascript is old but i used it to my site. It’s good and it’s not jquery.

    Hi @ Peter

    It’s quite easy, omit the index.php so instead of index.php?lang=en it would be better this way ?&lang=es and you won’t be redirected in the index.php for changing language! It will repeat the requested language over and over again but atleast it wort. Hope other users will find a better way and please mail me @ brixter.deleon[@]paper-soup.com.

    Hi @ Marzio

    It’s already in there. Take a good close look at line 40. If the script can’t find the language requested by the user, it will automatically call on the default which is the english. So when a user submitted lang?=justajoke, the script will automatically call in the default which is the english language.

    Anyways, nice tutorial kudos to the author.

    1. @Brixter
      Thanks for the help. I actually found many sites offering the same way you suggested here but it’s not what I want and I found another way to do this exactly how I want. Thanks!

  15. I wanted to know if it is possible to use this define method but instead of having text, direct it to an image. If i have a menu that is constructed of images, how would I come about doing that?

    Thanks.

  16. Ah what you mean is that instead of text links you used image links like those buttons?

    Here is the answer, you can define it like this:

    Follow above instructions, then create a image link like below:

    define(‘LANG_HEADING’, ”);

    Hope that clears out your question,

  17. define(‘LANG_HEADING’, ‘[img src="position.jpg" /]‘);

    change [ to < oky?

  18. Great article on adding multiple language support to php. If you don’t like having multiple language files, you can also get the same effect by having a single file that contains all translations in a single array. You can see a tutorial about this approach here:
    http://tips4php.net/2010/03/support-multiple-languages-in-php/

    1. @ laur: I guess your missing proper fonts. Or you can use CHECK LINK for profer displaying special characters in html.

  19. Hi

    Tell me how can i make to appear German character.
    Ex: In demo website on menu “About us”, if i change on German language it appear ? instead the special character. what can i do?

  20. Hi there,

    This is a great tutorial, but I have one question. Is it possible to fallback to the English text if the translated text for the chosen language is not available?

    Thanks!

  21. Great script! Would really like to get this to work.
    Standalone it works.
    http://www.mashamoto-imports.com/languages.php

    But when I try to work it into my site I’m getting header errors.

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at /opt/www/mashamotoltd2/web/www.mashamoto-imports.com/index.php:1) in /opt/www/mashamotoltd2/web/www.mashamoto-imports.com/common2.php on line 2

    Warning: Cannot modify header information – headers already sent by (output started at /opt/www/mashamotoltd2/web/www.mashamoto-imports.com/index.php:1) in /opt/www/mashamotoltd2/web/www.mashamoto-imports.com/common2.php on line 3

    Warning: Cannot modify header information – headers already sent by (output started at /opt/www/mashamotoltd2/web/www.mashamoto-imports.com/index.php:1) in /opt/www/mashamotoltd2/web/www.mashamoto-imports.com/common2.php on line 12

    These 3 lines give me the “headers already sent” error

    session_start();

    header(‘Cache-control: private’); // IE 6 FIX

    setcookie(“lang”, $lang, time() + (3600 * 24));

    Could anyone please tell me how to solve this?

    1. Arthur try this; cut “session_start();” from common2.php and paste it into index.php

  22. BTW, I just noticed that when I copy & paste the exact same code from index.php in a new file called index2.php it works flawless :s ?

    http://www.mashamoto-imports.com/index2.php

    What could be causing this? The code is exactly the same?

    1. Arthur, are you saving your web page as a UTF-8 or ANSI? UTF-8 will create that issue too. Try saving your page again but with a ANSI encoding.

  23. How can I use Chinese or Japanese characters?

  24. Thanks matey! I got it working :)

  25. I haven’t saw this error (as posted by Author) in my local apache server but it displays in the internet.

    Warning: Cannot modify header information – headers already sent by (output started at /opt/www/mashamotoltd2/web/www.mashamoto-imports.com/index.php:1) in /opt/www/mashamotoltd2/web/www.mashamoto-imports.com/common2.php on line 12

    I guess saving the file as ANSI works. thanks @ tempest.

  26. Hello is this script SEO friendly ?
    If it is then how i can make a sitemap icluding the 3 different languages ?

  27. Is there any way to make this SEO friendly?

  28. There’s no one to answer…..

  29. I have some problems with coding. after inserting php code ie does’nt correctly show css. But all other browsers showing css correctly. how can i fix it? web page http://www.aquastyle.az

  30. “As you can see I have used sessions to register the value of $lang. This way users can navigate through the whole site and see the content in the chosen language”

    The above seems not to work for me. I have 2 languages. If i switch on index.php from English to Dutch it translates the menu, but if I click on a menu item it opens the page with the menu in English. Why is it not opening the page with the menu in Dutch?

    1. I forgot to mention that the next page opens with the normal url, so not with page.php?lang=en or page.php?lang=nl.

      1. I got it. Simple solution.
        I removed the <a href> codes from my main file (index.php for example) and put them (plus "?lang=en" at the end of url)in each language (translation) php.

        like this:

        In "index.php"
        <li><?php echo $lang['MENU_HOME']; ?></li>

        In "lang.en.php"
        $lang['MENU_HOME'] = '<a href="index.php?lang=en" target="_self">Home</a>';

        In "lang.jp.php"
        $lang['MENU_HOME'] = '<a href="index.php?lang=jp" target="_self">%u30DB%u30FC%u30E0</a>';

        and now it's working!

    2. I would like to see this in action. This way I could help you with this problem. I have re-tested the script and it does work fine for me. So, consider giving me the URL to the page where you have used this script.

  31. Esta bien el manual para multiples idiomas,

    pero por ejemplo como seria si el usuario tiene una pagina web que busca desde la base de datos

    y lo imprime en la pantalla, como seria en ense caso el cambio del idioma?

    en mi caso

    y publica toda la linea de CATNAME que son 5 datos, como seria para cambiar eso?

  32. Hello! Good Post! :)
    But got an important question about the SEO aspect. How this method will influence the Google Spider?

  33. Hello ,

    I am php beginner. I want to develop website which having multi-languages(Ex. English,Thai,Swedish etc).

    I have data files that I inserted into database. By using that database how I develop website?

    please give me all configuration detatils. or if possible give me url of such examples.

    Thanks in advance…..

  34. Hello! This is great tutorial! Thank you!

    I want to realise a language switcher using your tutorial.

    I have 3 languages in the site. Only 2 languages should be shown and the third is active as content on the site.

    When is clicked other language, switcher show only non active languages.

    How can be realised? Thank you in advance.

  35. Great script and I’ve used it with succes. I want to change something, but I don’t know how to do it. I’ve tried lots of things but I’m just not good enough yet at php.

    I’m trying to use the browser user information to see where he is from and automatically redirect this person to this language.

    Reason being I get a lot of english mail from native customers.

    I don’t like to use javescript, so modifying the script is the only way. Who here could help me.

    There are many scripts out there, I just didn’t managed to implement one.

    http://www.google.com/search?q=php+redirect+language&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:nl:official&client=firefox-a

    I know it has something to do with $HTTP_SERVER_VARS['HTTP_ACCEPT_LANGUAGE']

    Thanks!!

  36. Im also wondering how to do that Arthur.

    Someone please help…

  37. This is a carefully detailed guide, and I really learned from it. I’ll be downloading the file also later. Thanks for sharing!

  38. D’oh. It’s really simple. Just edit common.php

    How could I’ve missed this :P

  39. Hi

    Sorry this has been asked earlier but what do I need to add so I can use images instead of text?

    1. In this tutorial, I’ve used images instead of text to switch to different languages. Do you want to show different images in the page for each language? If that’s what you want you can just embed the HTML code for the images you want in the languages pages, e.g.

      This could be set in lang.en.php:
      $lang['FRONT_PAGE_IMAGE'] = '<img src="http://www.mydomain.com/images/en/front-page.jpg&quot; width="500" height="200" border="0">';

      1. Hi, yes that was what I meant, however when I use the above code it just shows the code instead of the image. What I am I doing wrong?

        Thanks for the help

  40. Just want to say my thanks, your example is the best I personally found :)

  41. Hi,

    Great article, well done on the tutorial. However, 1 recurring question that I feel was not addressed in the thread is how this implementation gels with SEO and Google spiders? Is this a friendly solution for SEO purposes and if not, what extensions should be made to it in order to become more SEO friendly.

    Cheers

    1. If the content is the same, but written in another language, I don’t believe it should be a problem. In my opinion, it’s they are relevant results for Search Engines. I suggest you using friendly URLs such as http://www.yoursite.com/EN/page.html http://www.yoursite.com/ES/pagina.html. I would not be worried about duplicate content.

  42. why this doesn’t work?

    <?php if ( $lang == en ){ echo "choosed"; } else { echo "EN“; ?>

    1. Actually, it should be:

      <?php
      if ($lang == 'en') {
      echo 'chosen';
      } else {
      echo 'EN';
      }
      ?>

      1. Hey Gabriel, any chance of helping on the SEO question? Will a mod-rewrite fix the issue?

        1. Yes, if you know how to use it. I don’t suggest leaving it as it is. My tutorial is not complex and just covers some basic ways to use multiple languages in a website.

      2. Yes but this doesn’t work. Work only if i remove

        $lang = array();

  43. I tried to use yhis way at mt site
    http://www.thechaniavillas.com
    but i always fail to make the lang change work correctly

    i use this

    include "lang.class";
    if(empty($_GET['lang']))
    {
    if($_COOKIE['lang'])
    {
    $_GET['lang'] = $_COOKIE['lang'];
    }
    else
    {
    $_GET['lang'] = "en";
    setcookie("lang", 'en', time()+360000, '/', $_SERVER['SERVER_NAME']);
    }
    }
    else
    {
    setcookie("lang", $_GET['lang'], time()+360000, '/', $_SERVER['SERVER_NAME']);
    }
    $_lang = new Lang($_GET['lang']);

    but if you add the slash or anything after the index.php?lang=en or index.php?lang=gr

    i have error

    Warning: fopen(languages/gr1.lang) [function.fopen]: failed to open stream: No such file or directory in /home/thecscom/domains/thechaniavillas.com/public_html/lang.class on line 68

    Warning: fclose(): supplied argument is not a valid stream resource in /home/thecscom/domains/thechaniavillas.com/public_html/lang.class on line 80

    is there a way to beat this?

  44. This is part of the lang class i use


    /*
    * get the text from the file
    */
    function GetLanguages() {
    $lang_folder=opendir($this->directory);
    while (false !== ($file = readdir($lang_folder))) {
    if (($file!='.') and ($file!='..') and preg_match("/.lang$/",$file)) {
    $lines = file($this->directory."/".$file);
    $name = 'Ismeretlen';
    foreach ($lines as $line) {
    if(substr($line,0,5) == 'name ') {
    $this->languages[substr($file,0,strpos($file,'.'))] = substr($line,7,-1);
    break;
    }
    }
    }
    }
    }


    /*
    * Load the language file
    */
    function LoadLangFile() {
    $prefix = "";
    $file = $this->directory."/".$this->lang.".lang";
    $open = fopen($file, "r");
    if ($open) {
    while (!feof($open)) {
    $buffer = fgets($open, 4096);
    if((substr($buffer,0,1) == "\n") || (!substr($buffer,0,1))) continue;
    if(substr($buffer,0,1) == '[') $prefix = substr($buffer,1,-2);
    else {
    list($key,$value) = preg_split("/ = /", $buffer);
    $this->text[$prefix."_".$key] = substr($value,0,-1);
    }
    }
    }
    fclose($open);
    }


    /*
    * The Constructor
    * @parameters
    * $lang ~ tha code of language
    * $dir ~ base directory of language files
    */

    function Lang($lang, $dir = "") {
    $this->lang = $lang;
    if(!empty($dir)) $this->directory = $dir;
    $this->LoadLangFile();
    $this->GetLanguages();
    }
    }

    if some could help that would be very nice

  45. Wow.It’s nice to see the codes though its crazy a bit. Haha. Nice blog, i learned something some of the php codes, maybe i’ll apply it in the future :D

  46. I learned a lot, i have my programming language maybe i’ll apply it to my project in web programming. thanks for this nice blog.

  47. It makes me dizzy reading those codes, but i remember i have a friend who is a programmer. Maybe i’ll try to use this codes to showcase to him that i also learned something. Hehe. This is great :D

  48. Thank you, can i use these codes for my upcoming project in PHP subject? This is really nice. Thanks in advice, nice blog. :)

  49. Nice tutorial. thanx for posting.

  50. still no answer to my question though


Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)