How To: Make a website that allows for multiple languages

April 24th, 2009

So you have an old website or even a current one and you not have the need for several languages because of the traffic your getting. There are many ways of doing this and im giving a fairly simplistic method (simple is not a bad thing in this case). I will be doing this tutorial in a few programming languages but i will first show you in PHP as its my favourite :)

PHP METHOD

I will break this down into a few stages for you. Im going to assume that you have some header text you need to be multi lingual and the navigation links (but this is really irrelevant as you can use this method wherever you need). This is designed for the static content on your pages (i will cover a similar method for DB driven content as soon as someone requests it or i get chance!)

Breakdown of Tasks

  1. create languages folder in your web root (or wherever you want on your web server)
  2. create english.php file within that for starters
  3. create definitions within that file, one for each static piece of text
  4. create index.php and create drop down language selector
  5. include the correct language file dependant on the selected language (stored in session data)
  6. substitute your static bits of text with the relevant definition call
  7. Enjoy your multi language website :)

OK lets get started…

1. create languages folder in your web root (or wherever you want on your web server)

OK im going to recommend creating a new folder in your website root called languages. This will be used to hold all your language definition files.

2. create english.php definition file

I’m using English as my example but name this after one of the languages you require and create the blank file called english.php in the languages folder.

3. create definitions within that file, one for each static piece of text

Edit the new language files in your editor of choice and add the following code (use your own names to define areas on your site you get the idea)

1
2
3
4
<?php
define("agHeader", "Hello im a title", true);
define("agNavigationLink", "Hello im a link", true);
?>

save the file. Now make a copy of this file in the same folder and call it for example “polish.php” and keep the definition names the same THIS IS CRUCIAL. Change the values though to the new language see below for a polish example!

1
2
3
4
<?php
define("agHeader", "Czesc jestem tytuł", true);
define("agNavigationLink", "Czesc jestem hyperłączność", true);
?>

4. create index.php and create drop down language selector

Create a new index.php file outside of the language folder and within it we need to have a drop down list to select the language we require. Once this is selected we also need to store the language so that when we navigate away from the page the settings are remember. For this we will use sessions.

My Dropdown box code is like this:

1
2
3
4
5
6
7
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <select name="language">
        <option value="english"<?php if($_SESSION['language']=="english"){ echo " selected=selected"; } ?>>English</option>
        <option value="polish"<?php if($_SESSION['language']=="polish"){ echo " selected=selected"; } ?>>Polish</option>
    </select>
    <input type="submit" value="Change" name="submit" />
</form>

this will submit to itself when a language is selected and as you can see, it will automatically start at the current language using a quick check on the SESSION variable we are going to use.

5. include the correct language file dependant on the selected language

At the very top of the file i have started the session and done a couple of things. First check if the language has been posted via the form, if it has then it will be stored in a session variable. The next thing that is done is using the session data the language definition is included using a switch statement and not the form data directly for security reasons. We also use the PHP5 filter_input() function to be safe when setting the session data.

This is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
session_start();
if(isset($_POST['language'])){
$_SESSION['language']= filter_input(INPUT_POST, ‘language’, FILTER_SANITIZE_SPECIAL_CHARS); //filter the posted data just for security
}
 
switch ($_SESSION['language']) {
case “polish”:
include(”languages/polish.php”);
break;
default:
include(”languages/english.php”);
}
 
?>

6. substitute your static bits of text with the relevant definition call

Last step where you want your text to appear you simple echo out your definitions like so

1
<?php echo agHeader; ?>

7. Complete!

Thats it!! Below i’ve given a link to the example files and an online demo.

If you do download the example please comment and give feedback as i want to make my blog’s as useful as possible for everyone. If you need any more info or help just ask! I always have time for people if you are keen to learn :)

Please comment on the article and if you like it link back to me and digg it!

All feedback welcome, thank you

If you would like to download a basic example then please do so here

If you would like to view the online demo go here

If you are interested in learning more about PHP we highly recommend this book and it will really help you with your development: