Creating a Localized Page Structure

In this section, you look at a functioning example of a localized welcome page that uses PHP to enable a user to select a target language and then receive the appropriate text. The goal of this section is to show an example of externalizing the strings used in this script, which is one of the characteristics of internationalization.

In this script, the user happens upon your English-based website but is also presented with an option to browse within the locale of his choiceEnglish, German, or Japanese. Three elements are involved in this process:

  • Creating and using a master file for sending locale-specific header information

  • Creating and using a master file for displaying the information based on the selected locale

  • Using the script itself

Listing 27.1 shows the contents of the master file used for sending locale-specific header information.

Listing 27.1. Language Definition File

 1:   <?php 2:   if ((!isset($_SESSION["lang"])) || (!isset($_GET["lang"]))) { 3:       $_SESSION["lang"] = "en"; 4:       $currLang = "en"; 5:   } else { 6:       $currLang = $_GET["lang"]; 7:       $_SESSION["lang"] = $currLang; 8:   } 9: 10:  switch($currLang) { 11:      case "en": 12:           define("CHARSET","ISO-8859-1"); 13:           define("LANGCODE", "en"); 14:      break; 15: 16:      case "de": 17:           define("CHARSET","ISO-8859-1"); 18:           define("LANGCODE", "de"); 19:      break; 20: 21:      case "ja": 22:           define("CHARSET","UTF-8"); 23:           define("LANGCODE", "ja"); 24:      break; 25: 26:      default: 27:          define("CHARSET","ISO-8859-1"); 28:          define("LANGCODE", "en"); 29:      break; 30:  } 31: 32:  header("Content-Type: text/html;charset=".CHARSET); 33:  header("Content-Language: ".LANGCODE); 34:  ?>

Lines 28 of Listing 27.1 set up the session value needed to store the user's selected language choice.

By the Way

The session_start() function is not used in the define_lang.php or the lang_strings.php file listed in the following paragraphs because these files are included via the include() function from within the master file. The master file, which you will create shortly, calls the session_start() function, which will be valid for these included files as well.

If no session value exists, the English locale settings will be used. If your site were a German site by default, you would change this file to use the German locale by default. This script prepares for the next script, which contains an input-selection mechanism, by setting the value of $currLang to the result of this input in line 6.

The switch statement beginning on line 10 contains several case statements designed to assign the appropriate values to the constant variables CHARSET and LANGCODE. Lines 3233 actually utilize these variables for the first time when dynamically creating and sending the headers for Content-type and Content-language.

Save this file as define_lang.php and place it in the document root of your web browser. This file defines two constants that will be used in the next script, which is the actual display script. The constants are CHARSET and LANGCODE, corresponding to the character set and language code for each locale. These constants are used in the display script to create the proper META tags regarding character set and language code; although the headers are sent in this script, it's a good idea to ensure that they are part of the page itself to aid in any necessary input from forms.

Listing 27.2 creates a function that simply stores the externalized strings that will be used in the display script. This example uses two: one to welcome the user to the page (WELCOME_TXT) and one to introduce the language selection process (CHOOSE_TXT).

Listing 27.2. String Definition File

 1:  <?php 2:  function defineStrings() { 3:    switch($_SESSION["lang"]) { 4:        case "en": 5:            define("WELCOME_TXT","Welcome!"); 6:            define("CHOOSE_TXT","Choose Language"); 7:        break; 8: 9:        case "de": 10:            define("WELCOME_TXT","Willkommen!"); 11:            define("CHOOSE_TXT","Sprache auswählen"); 12:       break; 13: 14:       case "ja": 15:            define("WELCOME_TXT","[unprintable characters]"); 16:            define("CHOOSE_TXT","[unprintable characters]"); 17:       break; 18: 19:       default: 20:            define("WELCOME_TXT","Welcome!"); 21:            define("CHOOSE_TXT","Choose Language"); 22:       break; 23:    } 24: } 25: ?>

Use the file lang_strings.php from the CD included with this book, to use the actual Japanese characters that cannot be displayed here. Place this file in the document root of your web browser. This file defines two constants, WELCOME_TXT and CHOOSE_TXT, which are used in the display script. These constants are defined within the context of the function called defineStrings(), although you could just as easily make this file a long switch statement outside the context of the function structure. I've simply put it in a function for the sake of organization and for ease of explanation when it comes time to use the display script.

Finally, it's time to create the display script. Remember, one key element of internationalization is to externalize all strings so that only one master file needs to be used. Listing 27.3 is such an example.

Listing 27.3. Localized Welcome Script

 1:  <?php 2:  session_start(); 3:  include("define_lang.php"); 4:  include("lang_strings.php"); 5:  defineStrings(); 6:  ?> 7:  <html> 8:  <head> 9:  <title><?php echo WELCOME_TXT; ?></title> 10: <META HTTP-EQUIV="Content-Type" content="text/html; 11:     charset=<?php echo CHARSET; ?>"/> 12: <META HTTP-EQUIV="Content-Language" content="<?php echo LANGCODE; ?>"/> 13: <body> 14: <h1 align="center"><?php echo WELCOME_TXT; ?></h1> 15: <p align="center"><strong><?php echo CHOOSE_TXT; ?></strong><br/><br/> 16: <a href="<?php echo $_SERVER["PHP_SELF"]."?lang=en"; ?>"> 17:     <img src="/books/2/866/1/html/2/en_flag.gif" border="0"/></a> 18: <a href="<?php echo $_SERVER["PHP_SELF"]."?lang=de"; ?>"> 19:    <img src="/books/2/866/1/html/2/de_flag.gif" border="0"/></a> 20: <a href="<?php echo $_SERVER["PHP_SELF"]."?lang=ja"; ?>"> 21:    <img src="/books/2/866/1/html/2/ja_flag.gif" border="0"/></a> 22: </p> 23: </body> 24: </html>

You'll notice that Listing 27.3 is a basic template because all the language-related elements are externalized in the define_lang.php or lang_strings.php files. All this third file does is display the appropriate results, depending on the selected (or default) locale.

Line 5 calls the defineStrings() function, which then makes available the appropriate values for the two constant variables. Lines 1518 display the flags representing the English, German, and Japanese locales, which are clickable. When the user clicks on one of the flags, the locale will change to the new, selected locale, and the strings used will be those appropriate to the new locale. These links contain the lang variable, which is passed to the script as $_GET["lang"]. If you look at line 6 of Listing 27.1, you will see how this is used to change the setting regarding the user's preferred locale.

Save this file as lang_selector.php and place it in the document root of your web browser. When visited for the first time, it should look something like Figure 27.1.

Figure 27.1. Viewing the language selector for the first time.

Until another language is selected, the default is English; accordingly, the Welcome and Choose Language text appears in English. When the user clicks on the German flag, he will see Figure 27.2; when the user clicks on the Japanese flag, he will see Figure 27.3.

Figure 27.2. Viewing the German language page.

Figure 27.3. Viewing the Japanese language page.

The use of the flag of Great Britain might seem unusual for a book written by an American and with primary distribution in the United States. However, when flags (instead of names of countries) are used as locale selectors, it is more common for the English locale to be represented by the flag of Great Britain rather than the United States of America. As you can imagine, the use of flags for locale selection can get very political.

Companies that offer localized versions of their websites often have long discussions about how to represent the locale selectionsflags, names of countries, names of languages, and so forth. There is no clear-cut answer; the websites for Sun Microsystems, Cisco Systems, and IBM use country names, whereas other large corporations use language names, but even Google uses flags. How to display the language selection can definitely be a business decision, but if you have gone through the process of externalizing strings, text, and images and created an internationalized website template that is ready to be localized, the format of your locale selection is the least of your concerns.

Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327 © 2008-2017.
If you may any questions please contact us: