Create a multilanguage website with PHP

How to make your site compatible for translation in most different languages

When we begin with the development of a new internet site we should always think of making it multi-lingual, one because it’s multilingual and has virtually zero cost, the second because converting it to the multilingual, much more, while consuming a lot of time, and therefore cost.

Let’s start

The first thing we are going to create in the root of our site, a folder calling it as we want, generally I call it lang/, and inside we are going to create many php files, one for each language. We will begin with the Italian, then the work is finished it will be enough to translate the single file.

The file for the Italian language the name is generally it.php because I have never worked on sites as particularly important. You can call it as you want, also it_IT.php or italiano.php considering that the format for it_IT it is useful for sites of particular importance, which categorize a language based on the country, as for example the English that is slightly different in America or in England, identified with en_US or en_UK. Wanting to with this format, you can work on the forms of Italian dialect, as it_NA.php for the neapolitan, it_RM.php for the roman, or it_MI.php for the milanese, and so on. With a configuration of this kind have fun following the creation of the site in a moment.

We create our own dictionary file

As mentioned, we create in the root a folder chiamanta lang/, and inside some files for languages, called it.php, en.php, de.php


We open, for example, the file it.php and compose it more or less like this, for example

$dictionary = [];
$dictionary['lang.lingua'] = "Italiano";
$dictionary['txt.header.benvenuto'] = "Benvenuto";
$dictionary['txt.header.modificaprofilo'] = "Modifica profilo";
$dictionary['button.profile.aggiornaprofilo'] = "Aggiorna profilo";

then we have the english one en.php

$dictionary = [];
$dictionary['lang.lingua'] = "English";
$dictionary['txt.header.benvenuto'] = "Welcome";
$dictionary['txt.header.modificaprofilo'] = "Edit profile";
$dictionary['button.profile.aggiornaprofilo'] = "Update profile";

then we have the german one de.php

$dictionary = [];
$dictionary['lang.lingua'] = "German";
$dictionary['txt.header.benvenuto'] = "Willkommen";
$dictionary['txt.header.modificaprofilo'] = "Profil bearbeiten";
$dictionary['button.profile.aggiornaprofilo'] = "Profil aktualisieren";

If you have followed our lesson of The array in PHP by our guide, you will already know that the indexes of an array can be both numeric, identifying the position in the array is alphanumeric. We need to identify with the label, speaking the text that you want to use. In our case, the message in Italian “Welcome” is identified with the label txt.header.welcome. This label must correspond to the word Welcome in all the other languages, and so for all the other definitions.

We integrate the languages in the site

This work has to be done in the header. Then, add in the file header.php of our site the following code

// Identifichiamo la lingua, e se non esiste
// impostiamola di default in italiano
$lang = $_GET['lang'];
if (!isset($lang) OR !file_exists("lang/{$lang}.php")) {
    $lang = "it";

// Includiamo il file del linguaggio per

echo $dictionary['lang.lingua']."<br/><br/>";

// Aggiungo i link di cambio lingua
echo "Select your language:<br>";
echo "<a href='" . $_SERVER['PHP_SELF'] . "?lang=it'>Italiano</a> - ";
echo "<a href='" . $_SERVER['PHP_SELF'] . "?lang=en'>Inglese</a> - ";
echo "<a href='" . $_SERVER['PHP_SELF'] . "?lang=de'>Tedesco</a>";

Recall that the values in our site

In order to recall the values of the dictionary to our site we have to refer to the label that we have set in the dictionary file. To write Welcome use $dictionary[‘txt.header.welcome’], to write Edit profile we will use $dictionary[‘txt.header.editprofile’], and so on, and the code will automatically translate the site depending on the language of your choice.

To add a new language you just need to copy a file that already exists by calling it with the reference of the new language, and translating all the individual commands within it, and the site will be automatically translated.

It is clear that if we add a new definition of a language already existing, this should be reproduced in all other languages.

Offrimi un caffè su
We randomly check the functioning of the links in our articles. In the meantime, if any links are corrupt, please let us know in the comments, we will correct them. Thanks.


Founder and CEO of Dummy-X since 2007, I share interests, ideas, projects, and IT solutions. I do not sell products, I limit myself to describing technologies and making available the right tools to realize ideas. Everything I write is the result of my work experience and my desire to learn. I have always been studying computer science in all its angles (software, hardware, networks and security) and in recent years I have specialized in the development of web applications. I am always open to new challenges and to research and knowledge of new technologies.

Leave a Reply

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