Multilingual website means that supports multiple languages i.e If your website is accessed in USA, it will be in English, if it is accessed in India, it will be in Hindi, If it is accessed in France, it will be in French and so on.
In PHP we can easily develop a multilingual website. The concept behind this is very simple. We can use Cookie based approach to develop such a website.
Here are the steps to develop a multilingual website:-
- Get the location of the user using Maxmind database.
- Get the country code from the location.
- You can easily get country vs language database from internet.
- Now against the location, you can get the language code.
- Now set the cookie of the user with language code.
- Keep a common directory as lang in your root folder and have separate property files for each language for example for English keep one en.php, for Hindi keep hi.php and so on.
- Now include this language file on the run time so that your website content appears as per location of the user.
Here is the sample code to achieve this:-
The lang directry:-
$lang['ERR_USERNAME_REQ']=”Please enter Username”;
$lang['ERR_PASSWORD_REQ']=”Please enter Password”;
$lang['ERR_EMAIL_REQ']=”You have to enter your e-mail address”;
$lang['ERR_GENDER_REQ']=”You have to select your gender”;
$lang['ERR_USERNAME_EXISTS']=”Username is already in use”;
$lang['ERR_INVALID_EMAIL']=”Email address id not valid”;
Determining the location of the user and setting the language cookie:-
$giCity = geoip_open(“GeoLiteCity.dat”,GEOIP_STANDARD);
$ip = $_SERVER['REMOTE_ADDR'];
$result = geoip_record_by_addr($giCity, $ip);
$country_code = $result->country_code;
Including the language file:-
if(!isset($_COOKIE['lang']) || $_COOKIE['lang']==”")
Now following all these steps we can easily develop a multilingual website. Please keep in mind all the content, labels, error messages of your website need to be in the language specific file and your php should have the array variables instead of direct labels. For example instead of having
<label>Username<label>, you should use <label><?php echo $lang[LABEL_USERNAME’];?></label>
Thus all the content will be replaced when you include language specific files and your website will support multilingual feature.