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.

Advertisements

One thought on “PHP Multilanguage Site Using Arrays

  1. Thank you for the idea. I have created an array for each variable, that contains the translations for each lang in the same order like here: $welcome = array(“hello”,”bonjour”)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s