This blog is NOFOLLOW Free!
  • INCEPTION THEME

    Clean, contrasting WordPress theme built on the foundation of Whiskey Air.
  • WHISKEY AIR THEME

    Clean developer theme, suited for heavy modifications.
  • X5 TURBO THEME

    3 columns Turbo theme, now reloaded!
  • CLEAR APPLE THEME

    Almost magazine theme, without the hassle of administration.

PHP Multilanguage

A long time ago I wrote a small tutorial for multilingual web sites using PHP. That tutorial is a bit outdated and unoptimized. I recently had to turn a static localized CRM into a multilanguage one. The obvious solution, without relying on gettext functions was to use PHP 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('/php_multilanguage_site_using_arrays/includes/config.html');
include('/php_multilanguage_site_using_arrays/languages/index.html'.$lng.'/php_multilanguage_site_using_arrays/.html');

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 <?php echo $lang['REGISTER'];?>. It will display “Register”, just as you translated it in your language file.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>