#native_company# #native_desc#

Localizing a Web Page for Different Languages

By Eli White III and Jonathan Eisenhamer
on October 11, 2006

Internationalization and localization of a web page is simply the act of setting it up to be able to handle displaying in multiple languages and adding those different languages in. There are many different ways in which to do this. One of the simplest is to just make sure that all your strings that you ever output are stored as variables or constants in an included file. That way, you can make multiple copies of that file,each with different language versions written into them. Just include the appropriate file for the language that you want to display.
This idea can be extended to images as well because any image with text on it would also need translated. Just define the imgsrc of each image in your included file as well.
Listings 9.7.1, 9.7.2, and 9.7.3 are examples of what these included files could look like, given in three different languages.

Listing 9.7.1 Localization File for English??Filename: en.php

// Declare all text strings that we need,
// in English.
$GLOBALS['text'] = array (
  'welcome' => 'Welcome to our website!',
  'thanks' => 'Thank you for your patronage.',
  'sky' => 'The sky is falling!',
  'game' => 'Would you like to play a game?',
  'switch' => 'Switch the language to:',

// Now, also define alternative imgs for our use
$GLOBALS['imgsrc'] = array (
  'title' => 'graphics/title.en.png',
  'footer' => 'graphics/foot.en.jpg'

Listing 9.7.2 Localization File for French??Filename: fr.php

// Declare all text strings that we need,
// in French.
$GLOBALS['text'] = array (
  'welcome' => 'Bienvenue à
     notre site Web!',
  'thanks' => 'Merci de soutenir nos affaires.',
  'sky' => 'Le ciel tombe!',
  'game' => 'Aimez-vous jouer un jeu?',
  'switch' => 'Commutez la langue à:',

// Now, also define alternative imgs for our use
$GLOBALS['imgsrc'] = array (
  'title' => 'graphics/title.fr.png',
  'footer' => 'graphics/foot.fr.jpg'

Listing 9.7.3 Localization File for German??Filename: de.php

// Declare all text strings that we need,
// in German.
$GLOBALS['text'] = array (
  'welcome' => 'Willkommen zu unserer
     Web site!',
  'thanks' => 'Danke für Ihr Patronat.',
  'sky' => 'Der Himmel fällt!',
  'game' => 'Wurden Sie mögen ein
     Spiel spielen?',
  'switch' => 'Schalten Sie die Sprache zu:',

// Now, also define alternative imgs for our use
$GLOBALS['imgsrc'] = array (
  'title' => 'graphics/title.de.png',
  'footer' => 'graphics/foot.de.jpg'
Now that we have all the different language versions saved in separate files, we want to make it easy for any page to use these. Therefore, a library should be created to handle this. The library shown in Listing 9.7.4 when included in a web page automatically detects what language should be used based on a GET string or a cookie. It also provides a function for easily creating some XHTML that allows for switching languages.

Listing 9.7.4 Localization Library??Filename: language.php

// This is a library, that by including it,
// automatically determines the proper language
// to use and includes the language file.

// First define an array of all possible
// languages:
$languages = array('en' => 'English',
  'fr' => 'French', 'de' => 'German');

// Look at the GET string to see if lang is
// specified:
  if (isset($_GET['lang'])) {
  // It's been specified, so set the language
  $lang = $_GET['lang'];
  // While here, send a cookie to remember this
  // selection for 1 year.
  setcookie('lang', $lang, time()+(3600*24*365));
// Ok, otherwise look for the cookie itself:
elseif (isset($_COOKIE['lang'])) {
  // Use this
  $lang = $_COOKIE['lang'];
} else {
  // Otherwise, default to English
  $lang = 'en';

// Make sure that the language string we have is
// a valid one:
if (!(in_array($lang, array_keys($languages)))) {
  die("ERROR: Bad Language String Provided!");

// Now include the appropriate language file:
require_once "{$lang}.php"

// As one last step, create a function
// that can be used to output language
// options to the user:
function switch_language_options() {
  // Include a few globals that we will need:
  global $text, $languages, $lang;

  // Start our string with a language specific
  // 'switch' statement:
  $retval = $text['switch'];

  // Loop through all possible languages to
  // create our options.
  $get = $_GET;
  foreach ($languages as $abbrv => $name) {
    // Create the link, ignoring the current one.
    if ($abbrv !== $lang) {
      // Recreate the GET string with
      // this language.
      $get['lang'] = $abbrv;
      $url = $_SERVER['PHP_SELF'] . '?' .
      $retval .= " <a href="{$url}">

// Now return this string.
  return $retval;
With this completed, it can be easy to generate a web page that is language independent, such as Listing 9.7.5.

Listing 9.7.5 Localization: Complete Example

// Include our language library:
require_once 'language.php';
 XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<title><?= $text['welcome'] ?></title>
<meta http-equiv="Content-Type"
  content="text/html; charset=iso-8859-1" />
<p><img alt="<?= $text['welcome'] ?>"
  src="<?= $imgsrc['title'] ?>"
height="50" width="500" /></p>
<h2><?= $text['thanks'] ?></h2>
<p><?= $text['game'] ?></p>
<p><img alt="<?= $text['sky'] ?>"
  src="<?= $imgsrc['footer'] ?>"
height="10" width="220" /></p>
<p><?= switch_language_options(); ?></p>
Note: PHP also provides an interface to the GNU gettext library. This is a generic library created to handle internationalization/localization problems. Using it requires building the support for it into PHP and installing the gettext library on your computer. If you want to learn more about this, visit http://php.net/gettext.

This article is taken from the chapter titled, “Web Page Creation/XHTML/CSS”, which is excerpted from the new book, PHP 5 in Practice, authored by Eli White III and Jonathan Eisenhamer. Copyright 2007 by Sams Publishing. ISBN 0672328887. Reprinted with permission by Pearson Education; All rights reserved. A complete Table of Contents is available online.