Here I am to show you a PHP script to be able to view your site in multiple languages. An alternative technique would be the “splitting of the site” on two separate folders of files (which can become too many to manage) at the expense of a fundamental point of computer science: the reusability of the code.
This system allows you to reach a greater number of users translating your texts. First of all, insert the language flags in our files to be translated.
<ul id="lang">
<li>
<a id="it" class="bandiere" href="modificaLang.php?lang=it"></a>
</li>
<li>
<a id="en" class="bandiere" href="modificaLang.php?lang=en"></a>
</li>
<li>
<a id="es" class="bandiere" href="modificaLang.php?lang=es"></a>
</li>
<li>
<a id="fr" class="bandiere" href="modificaLang.php?lang=fr"></a>
</li>
</ul>
And we assign the CSS to show the flags as the background of the links.
#lang {list-style: none}
#lang li {float: right}
.bandiere {
display: block;
width: 36px;
height: 24px;
}
#it {background-position: 0 0}
#en {background-position: 0 -37px}
#es {background-position: -25px 0}
#fr {background-position: -25px -37px}
We need to create the files that will act as dictionaries for the various languages. Ini files that will contain simple key = value
strings like the ones shown below.
File it.ini:
[text]
saluto = "Ciao."
benvenuto = "Benvenuto in questa demo."
File en.ini:
[text]
saluto = "Hello."
benvenuto = "Welcome to this demo."
File es.ini:
[text]
saluto = "Hola."
benvenuto = "Bienvenidos a esta demo."
File fr.ini:
[text]
saluto = "Bonjour."
benvenuto = "Bienvenue à cette démo."
And now let’s deal with the php part. First of all let’s create a php file to be included (with the require method) in all the pages with translated language, possibly among the <head>
tags. This file will be called for example lang.php. In this file we will read the system variable $_SERVER["HTTP_ACCEPT_LANGUAGE"]
which contains the user’s system language. This variable is part of an associative array that contains various information about servers and clients and their connection. We extract the language via the PHP function substr
<?php
$lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0, 2);
?>
Since we will give the user the possibility to choose the language, we create a cookie so that it keeps track of the chosen language and an if
to check if the cookie is already present. If the cookie is not present, one will be created with the language just found.
<?php
if (!isset($_COOKIE["lang"])){
$lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
setcookie("lang", $lang, time()+9000000,"/", "sito");
} else
$lang = $_COOKIE["lang"];
?>
With a switch we check that the variable is what we expect. If not, the English one will be set by default.
<?php
switch ($lang){
case "it":$lang = "it";break;
case "en":$lang = "en";break;
case "es":$lang = "es";break;
case "fr":$lang = "fr";break;
default:$lang = "en";
}
?>
And now let’s call the function that will take care of reading the required language file and generate an associative array containing key/value binomials of the sentences.
<?php
$LANG=parse_ini_file("..[path]/lang/".$lang.".ini",TRUE);
?>
If we then want the flag of the selected language to disappear, insert at the end of this file this echo that will insert a line of CSS that will hide the flag with the id of that language.
<?php
echo "<style type=\"text/css\">#".$lang."{display:none}";
?>
Now let’s create another file that will be used to change the language in case the user wants to use the site in a different language. We call this file modificationLang.php and we insert very few lines into it.
A switch will extrapolate the language id from $_GET
(avoiding intrusions) and insert it in the cookie. Finally it reports on the main page where the modified cookie will be read and then the chosen language will be used.
<?php
switch ($_GET["lang"]){
case "it":$lang = "it";break;
case "en":$lang = "en";break;
case "es":$lang = "es";break;
case "fr":$lang = "fr";break;
default:$lang = "en";
}
setcookie("lang",$lang,time()+9000000,"/","sito");
header("Location: demoLang.php");
exit;
?>
Finally in our pages we will insert, in place of each string, the variable of the array that will display the string.
<?=$LANG["text"]["saluto"] ?>
Here is a complete system for creating a multilingual system for our site. Advice or opinions are welcome.
See you until next time.