PHP Multilanguage Site Using Arrays

PHP Multilanguage

Here we can develop multilanguage site with the use of arrays

So, here we go:

First of all we need a configuration setting inside our script’s config.php file. For the sake of our example, we’ll add a manual variable change:

// language settings
$lng = ‘en’;

Next, our header file, dubbed header.php (could be head.php or top.php in your script) will contain these lines:

include(‘includes/config.php’);
include(‘languages/’.$lng.’.php’);

You now, obviously, have to create a languages directory and create a new file called en.php. This file will hold our array of words and expressions:

/*
@Package: My Multilanguage Script
@Language: English (English)
*/

$lang = array();

$lang[‘REGISTER’] = ‘Register’;
$lang[‘USERNAME’] = ‘Username’;
$lang[‘PASSWORD’] = ‘Password’;
$lang[‘LOGIN’] = ‘Log in’;
$lang[‘LOGOUT’] = ‘Log out’;
$lang[‘DASHBOARD’] = ‘Dashboard’;

Notice how I tried to keep the array index name as close to translation as possible. For example, you’ll have the string “Separate tags with commas” as $lang[‘SEPARATE_TAGS_COMMAS’]. It’s easier after a couple of months when you’ll make changes.

Also, try to keep consistent naming of your language files, such as fr.php, de.php, ru.php, cn.php.

Now, call in you script . It will display “Register”, just as you translated it in your language file.