Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

How To Implement a multi-language site/application.

Marco GasiFreelancer
CERTIFIED EXPERT
Freelance, I like to share what I know. Find out my articles in my learner-to-learners blog https://codingfix.com
Published:
I wish here explain how you can easily implement a multi-language site or application using Php.

Developing desktop applications for Windows, there are several method to accomplish this goal: for instance, you can use dynamic link library to hold a resource file with contains all text strings translated in a certain language; or you can "entrap" this resource file directly in the exe file of your program; or you can use several external files, one for each language you wish make available in your application, wich are loaded with a single click of mouse.

In Php we have no dll nor exe files, but we can store resource files within, so the more efficient, and as far as I know, the only one method to implement a multilanguage UI is the third one: write a file for each language we want to support and load it dynamically.

This is very easy to implement in Php using include directive and some language files which define constants for all text we have to insert in our site/app.

Lets start from here. Using your favourite text editor create a new file and name it as english.lang. In this file we will write our text this way:
define("pagetitle", "Multi-language application/site example.");

Open in new window

We will have to define how many constant as are the text strings of our site/app... I hear you saying: "but this is terribly annoying!" Oh yes, it is. But since we haven't a magic wand, this is the only way to do it. Once you have written your english.lang, it's time to write another lang file, for instance italian.lang (but obviously you can choose the language you prefer: I choose Italian because... I'm Italian!). In the archive you find two lang file, italian.lang and english.lang ready to use to test my example.

Have you done? Perfect. Now, we go to write the main file of our application.

The first thing we have to do is  to give to our users the possibility to choose their preferred language, so we write something like this:
<table>
                          <tr>
                              <td>
                              <form id="" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
                                  <input type="hidden" id="" name="lang" value="english" /><input 
                                  class="flagen" type="submit" id="submit" name="submit" value="" />
                              </form>
                              </td>
                              <td>
                              <form id="" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
                                  <input type="hidden" id="" name="lang" value="italian" /><input 
                                  class="flagit" type="submit" id="submit" name="submit" value="" />
                              </form>
                              </td>
                          </tr>
                      </table>

Open in new window

Notice the hidden field to pass lang value when form is submitted. Also notice that submit button has a class attribute: this is to give it an appropriate flag image as background (for this you can see multilang.css file in the archive).

So, when user chooses his favourite language, page is reloaded and choosen language is used. In fact at the top of the page we have following code
<?php
                      session_start();
                      if (isset($_POST['lang'])){
                          $lang = $_POST['lang'];
                      }elseif (isset($_SESSION['lang'])){
                          $lang = $_SESSION['lang'];
                      }else{
                          $lang = 'english';
                      }
                      $_SESSION['lang'] = $lang;
                      include ($lang.".lang");
                      }
                      ?>

Open in new window

First, we start a php sesison so $lang variable will be kept during all session without other efforts.
Second, we find an if statement wich assign to $lang its value:
 - $_POST['lang'] if user has clicked the lang flag to submit his choice;
 - $_SESSION['lang'] if the session variable has been set;
 - a default value in other cases.
Third, we save the $lang value to a session variable.
Fourth, and last, we include the correct lang file.

Now, to obtain our site/app use the right language we don't have to write any text in our files/pages, but use instead some placeholders, that is the constants defined in our lang files.

So, following our example of pagetitle constant (See above), we'll write:
<title><?php echo pagetitle ?></title>

Open in new window

Now the title of our page will be displayed in the correct language. Let me do another example. Suppose to have defined a constant to display some text to invite our user to choose a language:
define("chooselang", "Choose language: "); (english.lang)
                      
                      define("chooselang", "Scegli la lingua: "); (italian.lang) 

Open in new window


In the form we'll write this
<table>
                          <tr>
                              <td>
                              <?php echo chooselang ?>
                              </td>
                              <td>
                              <form id="" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
                                  <input type="hidden" id="" name="lang" value="english" /><input 
                                  class="flagen" type="submit" id="submit" name="submit" value="" />
                              </form>
                              </td>
                              <td>
                              <form id="" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
                                  <input type="hidden" id="" name="lang" value="italian" /><input 
                                  class="flagit" type="submit" id="submit" name="submit" value="" />
                              </form>
                              </td>
                          </tr>
                      </table>

Open in new window

So our text will be displayed in the correct language. And so on.

In the archive attached you'll find a complete example: just extract it to your server document root and play with it. Hope you find it interesting.  
 [embed=file 346446]

Open in new window

multilanguage.exe
3
5,460 Views
Marco GasiFreelancer
CERTIFIED EXPERT
Freelance, I like to share what I know. Find out my articles in my learner-to-learners blog https://codingfix.com

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.