How to Add PHP Multilingual Support to a Website

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


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.en.php, and 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:


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';

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';

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:

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

$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'];
$lang = 'en';

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

  case 'de':
  $lang_file = '';

  case 'es':
  $lang_file = '';

  $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

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

Comment via Facebook



  1. andy c says

    “As you can notice, some constants are created using the define() function”

    Sorry, but I don’t see this? I thought the ‘define’ structure was

    define(“CONSTANT”, “Hello world.”);

    which isn’t used in the lang.xx.php files.

  2. mohit says

    I have created multilanguage feature on our project, its working fine, but now in coding file
    it shows diffirent font for russian language.

    $lang[‘Email Notification’]=’Уведомление по электронной почте’;

    but correct means in russian language is :
    $lang[‘Email Notification’]=’Уведомление по электронной почте’;

    How this is possible to show me correct font as per language

  3. Tuga says

    this software works with the front page where the language is installed click the language and changes perfect

    when you navigate on other pages the language don’t change at all

    i would like to ask for any help of language change on all the pages if possible
    sooner change language on the front page all the other pages will change language as well
    its not working with this

  4. RIck says

    its a great tutorial but only change the languages on the first page

    using other pages not working the language change

    i had tried few options not working at all
    first page works perfect

    i want to load from the first page change on first page
    and all the other pages change the language as well

    if anyone can help me out


    • solovi says

      hi soravit I notice that you seem to have fix your problem in your website, since I have the same problem could you explain how you resolve it, thanks.

  5. says

    Saved me hours and hours of work. And one look it tells me that it can easily be converted for $l_files = etc and should blend in nicely with what I am trying to do.

    Thanks for the script, now all I have to do is apply it to my games demo site.

  6. says

    Hi :) I’ve been using this script for a while and really like it.

    I just wanted to know how to make it automatically detect the language of the browser or the OS, cause everytime i visit my website, it display in English.

    Thanks 😀

  7. dasky says

    I’m a rookie in designing websites and I love this tutorial.
    But I have a question according to the links in the header.

    So if I’m correct I just replace for example the ‘#’ in the header menu (menu_home) to ‘./index.php’. Can I just rely on the language cookie that has been set, or do I have to add something extra at the back like ‘./index.php?lang=[..] to keep it on the right language?

    I apologize if it was a dumb question.

  8. says

    Thanks for this tut!

    I have some issue’s though. Can’t figure it out. First of all, let me say that the folder & file structure is the same as in the demo.

    But somehow it just doesn’t show the languages. I started over like 4 times, but can’t find what’s wrong…

    I have altered the ‘common.php’ to make the default language dutch and the available languages, but nothing drastic:

    Now when I go to my website, it shows every element, but not the texts. The Dutch texts are only showing a D, and the English texts only an E.

    Furthermore I get an error that the include (as called on line 41 of the common.php) can not be performed?

    Anybody an idea? Suggestions are more then welcome!


    • Floris says

      I have the same problem too, with dutch language. If I switch "include_once ‘common.php’;" to "include_once ‘languages/lang/en.php’;" it doesn’t show anything… So, the problem isn’t common.php, but the request in the index or the language files, I guess…

    • Gabriel C.Gabriel C. says

      The requested value has to be checked. If something else is typed in the URL bar such as /?lang=foo then the include_once function will trigger an error. That’s why the switch case is added: for verification!

  9. Mauro says

    Is it possible to output the language data from a database? Instead of controllin the data directelly in the php language page making it communicate in the database. Whit the finality of controlling it in the backoffice.

  10. hasan says

    The script is able to be mounted on wordpress,
    to the system how, if the web owner wants to rewrite the language in translated or all languages ​​will automatically change all when the flag at the click ..?
    Thank you very much

    • Gabriel C.Gabriel C. says

      The script is just a basic example of how you can make a website multi-lingual. It’s not a good idea to mount it on WordPress, as there are other alternative methods especially made to work with WordPress. Take a look at the following post: (you can Google more about it as well “wordpress multiple languages”)!

  11. Peter says

    Thanks a lot for this great tutorial!!

    2 little questions:
    – is there a way to change the code in a way that when changing the language, the user stays on the same page instead of jumping to the index page?
    – how can special caracters be displayed they look kind of funny (even in the example)

    thanks a bunch

      • Tom says

        Another way to solve the special character problem is by using htmlentities() arround each of the strings with special chars. This is a safer way, as a browser may display UTF-8 characters as non-UTF-8, even when the file is saved as UTF-8.

        • John says

          How do you use html entities.
          This is what I came up with :
          $lang[‘PAGE_TITLE’] = htmlentities(‘Titre en Français’);

          but it doesn’t work !!


  12. Justin says

    This tutorial was great, but sort of skipped over the entire issue of encoding + how to integrate languages such as Russian / Chinese….

  13. Daniel Medina says

    Thank you for this tutorial / article.
    I am learning PHP and this gave me a mighty help to what I do.

  14. Makcum says

    I have used this code with 4 lang website, and having problem:

    You are on different from default ENG Language page and want to go to another page DE for example,
    going on DE page resets this page back to default ENG language and to see it again on DE or any other lang u have to press again.

    • George says

      Hello, I’m having this problem also.
      A have a small site with pages like index.php, contact.php, product.php. In each page a include menu.php and in menu.php a include common.php.

      I think is a problem with session_start() but i dont know how to fix it.

    • Xenn says

      I try to add to, for example, something like this:

      $lang_url = '?lang=de';

      The same I use in other languages and than for links in nav I use this:

      href="ponuka.php<?php echo $lang_url; ?>"

      I know that isn’t professional solution but it works for me :)

  15. James says

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

  16. Natalie says

    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 😀

  17. Rose says

    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.

  18. Alice says

    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 😀

  19. says

    This is part of the lang class i use

    * get the text from the file
    function GetLanguages() {
    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);

    * 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);

    * 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;

    if some could help that would be very nice

  20. says

    I tried to use yhis way at mt site
    but i always fail to make the lang change work correctly

    i use this

    include "lang.class";
    $_GET['lang'] = $_COOKIE['lang'];
    $_GET['lang'] = "en";
    setcookie("lang", 'en', time()+360000, '/', $_SERVER['SERVER_NAME']);
    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/ on line 68

    Warning: fclose(): supplied argument is not a valid stream resource in /home/thecscom/domains/ on line 80

    is there a way to beat this?


Leave a Reply

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