Support multiple languages in php

If your website has an international audience, it’s important that you also have a plan for how to support the multiple languages that your users might speak. Depending on how big a part of your site that needs to support multiple languages, different strategies can be suitable.

In this post you’ll learn how to make your php site support multiple languages.

There can be several ways of implementing multi language support on your site. You can add the different languages in a database, create a language file for each language that you wish to support, or create a common language file.  In this post we’ll work with an example file (index.php) and a common language file (lang.php).  This approach is working fine for small to medium sized sites with a relatively small amount of text, and a low number of languages supported.

Let’s first look at the basic example file:

<html>
  <head>
    <title>Multi language page</title>
  </head>
  <body>
    <p>Welcome English speaking user</p>
  </body>
</html>

To make this very simple example page language independent, all texts needs to be parameterized:

<html>
  <head>
    <title><? echo "{$m[''.$lang.'']['pagetitle']}"; ?></title>
  </head>
  <body>
    <p><? echo "{$m[''.$lang.'']['welcome']}"; ?></p>
  </body>
</html>

The philosophy behind the parameter name convention is that a English page title should be found in the language array with the following structure: m => language => textname. The naming of the different text pieces is very important, since the amount of text pieces can get very large, even on a relatively small site. Our recommendation is to include page title in the parameter name, in the cases where the text is page specific. The welcome text on the front page could e.g. be named: FrontpageWelcome.  

Example identification of the pagetitle in English and German:

English page title: m => en => pagetitle

German pagetitle: m => de => pagetitle

The advantage of having all texts in a array like this is, that you can easily introduce new languages on your site, it’s just a matter of translating all the values in the array to a new language. In this example we’ll support english (en), german (de) and spanish (es).

Copy the following array into the lang.php file:

<?
$m = array(
  'en' => array(
    'pagetitle'=>'Multi language page',
    'welcome'=>'Welcome english speaking user'
  ),
  'de' => array(
    'pagetitle'=>'Mehrsprachig Seite',
    'welcome'=>'Willkommen Englisch sprechende Benutzer'
  ),
  'es' => array(
    'pagetitle'=>'Pagina Multiples idiomas',
    'welcome'=>'Bienvenido Ingles hablando usuario'
  )
);
?>

Finally for the test to work, you need to include the language file in the example file:

<html>
  <head>
    <title><? echo "{$m[''.$lang.'']['pagetitle']}"; ?></title>
  </head>
  <body>
    <p><? echo "{$m[''.$lang.'']['welcome']}"; ?></p>
  </body>
</html>

And there you go, the example file now support 3 languages. Open index.php?lang=en for the English version, index.php?lang=de for the German version, and index.php?lang=es for the Spanish version.

Please notice that this guide only works with the texts on your site. You should remember to check if some of your graphics contains words in specific languages, that requires language versions of the images.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

5 Replies to Support multiple languages in php

  1. David says:

    HI, jhnidk

    I’m currently in the process of researching the best way to build my site in 3 languages and the method you have described appeals to me.

    Unfortunately I have tried to put your demo together but I must be doing something wrong, or misunderstanding some of your instructions, as each time I try it, I get the file returned without any of the language areas filled in!

    Your instructions seem clear, so I can’t figure out why it’s not working, I’ll paste my code below:

    array(
    ‘pagetitle’=>’Multi language page’,
    ‘welcome’=>’Welcome english speaking user’
    ),
    ‘de’=>array(
    ‘pagetitle’=>’Mehrsprachig Seite’,
    ‘welcome’=>’Willkommen Englisch sprechende Benutzer’
    ),
    ‘es’=>array(
    ‘pagetitle’=>’Pagina Multiples idiomas’,
    ‘welcome’=>’Bienvenido Ingles hablando usuario’
    )
    );
    ?>

    Any help you can offer would be much appreciated.

    Also, would you recommend this method for translating an entire website? I’m very new to php, so I’m looking for a method which will be simple to create, but flexible enough for me to use for a full site. For example, if I wanted to add any html to the text (like a link for example), is that going to be possible using this method?

    Thanks for your assistance.

    All The Best: Dave

  2. jhnidk says:

    Hi Dave

    The method suggested here is best for smaller sites that aren’t text heavy. If you’r site is very big, or contains much text you might be better off using a database.

    Anyway in your example you’re missing a few hierachies in your language array:
    array(
    ‘pagetitle’=>’Multi language page’,
    ‘welcome’=>’Welcome english speaking user’
    ),
    ‘de’=>array(
    ‘pagetitle’=>’Mehrsprachig Seite’,
    ‘welcome’=>’Willkommen Englisch sprechende Benutzer’
    ),
    ‘es’=>array(
    ‘pagetitle’=>’Pagina Multiples idiomas’,
    ‘welcome’=>’Bienvenido Ingles hablando usuario’
    )
    );

    Should be:

    m=array(
    ‘en’=array(
    ‘pagetitle’=>’Multi language page’,
    ‘welcome’=>’Welcome english speaking user’
    ),
    ‘de’=>array(
    ‘pagetitle’=>’Mehrsprachig Seite’,
    ‘welcome’=>’Willkommen Englisch sprechende Benutzer’
    ),
    ‘es’=>array(
    ‘pagetitle’=>’Pagina Multiples idiomas’,
    ‘welcome’=>’Bienvenido Ingles hablando usuario’
    )
    );

    and when you want to display the welcome text you need the following php code:

  3. David says:

    Cheers Jhnidk, don’t know how I missed that!

  4. diafol says:

    Hi, nice piece. As already mentioned, good for small sites. DB storage could be a solution, but tends to be difficult to maintain when adding new languages. A separate file for each language, holding an array with identical keys should be easier. Pages just include the relevant language file.

    In addition, any decent sized multilingual site may benefit from a templating engine – so you can use {$p1} as opposed to echo $lang[‘p1’];?> . RainTPL is a relatively painless way to achieve this.

  5. Shehzad says:

    Nice tutorial.Thanks

Comments are now closed for this article.