How to Create Multilingual Website in PHP

Adding multilingual/multi-regional support to a website can be particularly challenging. Since you may have multiple versions of your site, any issues will be multiplied. Offering content in several languages can add many layers of complexity to web development. Translating articles is only one of them. To structure a multilingual website properly you have to follow several best practices for multilingual websites.


The first thing that is needed to create a multi-language PHP website is to create a couple of files that contain the text for each language that is supported by the site. You have to use a class that allows implementing multiple languages to the site developer by the PHP language.


php_multiple_language_support_for_websites

The language class detects the browser automatically and displays the site content in the correct language and also helps to implement a system to change the language. These checks the HTTP request User-Agent and Accept Language headers and determine the users preferred language. It also loads the PHP scripts with locale text definitions for the preferred language.


I recommend to Use a framework or CMS. It doesn’t really matter which one – all the big names support this really well. I’ve built multi-lingual sites in Drupal and Symfony2, and their built-in support has made it really easy. There’s already a suggestion for Joomla – I’ve not used it, but it probably also has internationalisation support baked in. Don’t try and build it yourself in raw PHP. There are so many variations you would never even imagine in different languages.

Steps to create multilingual php website.

first build your data files, example :

//en_US.csv
 
hello_customer;Hello dear customer;
special_price;Special Price;
 
//fr_FR.csv
 
hello_customer;Bonjour cher client;
special_price;Prix spécial;

Then install and configure redis (key / value in memory database).
Get the extension (for instance for php : https://code.google.com/p/phpredis/)
Now build a script to populate your redis database. Maybe something along these lines:

<?php
$csv_path = 'path/to/csv/files';
 
function redis_import($f_name){
    $handle = @fopen($f_name, "r");
    $redis = new Redis();
 
    $lang = explode('.', basename($f_name))[0];
 
    if ($handle) {
        $redis->connect('127.0.0.1', 6379);
 
        while (($buffer = fgets($handle, 4096)) !== false) {
            list($key, $value) = explode(';', trim($buffer));
            $redis->set($lang.'.'.$key, $val);
        }
        if (!feof($handle)) {
            echo "Erreur: fgets() a échoué\n";
        }
        fclose($handle);
    }
 
}
 
if ($handle = opendir($csv_path)) {
    while (false !== ($entry = readdir($handle))) {
        if (!is_dir($entry)) {
            redis_import($csv_path.PATH_SEPARATOR.$entry);
        }
    }
    closedir($handle);
}



So whenever the csv files are modified, launch this script to populate the database.
Then you’ll need a function in your script to fetch language data.
You could use sessions to store user language. Assuming you used $_SESSION[‘user.lang’] to do so, you could craft something like this :

function __translate($key){
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379);
    $translation =  $redis->get($_SESSION['user.lang'] . $key);
    $redis->close();
    return $translation;
}

This solution is very fast there are just memory accesses, and still with the usage of csv files the real database is safe, and easily human accessible.

IF you looking for PHP Multilingual Libraries or Examples for your web based application. If yes then in this post I am going to share hand picked top rated PHP Multilingual Libraries or Examples. You can use these popular PHP Multilingual Website Libraries or Examples to make your web application perform on multi language.


35+ PHP Multilingual Libraries or Examples

Following are the list of popular top rated hand picked PHP Multilingual Libraries or Examples.

  • cms:- Multilingual PHP CMS built with Laravel and bootstrap
  • Platform:- A modular multilingual CMS built with Laravel 5.
  • package-metadata:- Multilingual package information for the Contao Manager
  • linguist:- Easy multilingual urls and redirection support for the Laravel framework
  • dokuwiki-plugin-translation:- Easily setup a multi-language DokuWiki
  • Localization:- Localization package for Laravel 5
  • laravel-translator:- An Eloquent translator for Laravel
  • realurl_404_multilingual:- This TYPO3 extension creates a multilingual 404 page with realurl
  • babel:- Babel is an Extra for MODx Revolution that creates linked documents across different contexts. The easy way for your m…
  • yii2-translate-manager:- Translation Manager

I Hope you liked Hand-picked list of PHP Multilingual Website Libraries or Examples, Don’t forget to Subscribe My Public Notebook for more useful Hand-picked PHP and MySql code examples, tutorials and articles.

Posted in PHP