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
In this script, the user happens upon your English-based Web site 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:
Listing 26.1 shows the contents of the master file used for sending locale-specific header information. Listing 26.1. Language Definition File
1: <?php
2: if ($_SESSION[lang] == "") {
3: $_SESSION[lang] = "en";
4: $currLang = "en";
5: } else {
6: $currLang = $_GET[lang];
7: $_SESSION[lang] = $currLang;
8: }
9: switch($currLang) {
10: case "en":
11: define("CHARSET","ISO-8859-1");
12: define("LANGCODE", "en");
13: break;
14:
15: case "de":
16: define("CHARSET","ISO-8859-1");
17: define("LANGCODE", "de");
18: break;
19:
20: case "ja":
21: define("CHARSET","UTF-8");
22: define("LANGCODE", "ja");
23: break;
24: default:
25: define("CHARSET","ISO-8859-1");
26: define("LANGCODE", "en");
27: break;
28: }
29:
30: header("Content-Type: text/html;charset=".CHARSET);
31: header("Content-Language: ".LANGCODE);
32: ?>
Lines 28 of Listing 26.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, as 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
The
switch
statement beginning on line 9 contains several case statements designed to assign the appropriate values to the constant
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 26.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 26.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 26.3 is such an example. Listing 26.3. Localized Welcome Script
1: <?
2: session_start();
3: include("define_lang.php");
4: include("lang_strings.php");
5: defineStrings();
6: ?>
7: <HTML>
8: <HEAD>
9: <TITLE><? echo WELCOME_TXT; ?></TITLE>
10: <META HTTP-EQUIV="Content-Type" content="text/html; charset=<? echo CHARSET; ?>">
11: <META HTTP-EQUIV="Content-Language" content="<? echo LANGCODE; ?>">
12: <BODY>
13: <h1 align=center><? echo WELCOME_TXT; ?></h1>
14: <p align=center><strong><? echo CHOOSE_TXT; ?></strong><br><br>
15: <a href="<? echo $_SERVER[PHP_SELF]."?lang=en"; ?>"><img src="en_flag.gif" border=0></a>
16: <a href="<? echo $_SERVER[PHP_SELF]."?lang=de"; ?>"><img src="de_flag.gif" border=0></a>
17: <a href="<? echo $_SERVER[PHP_SELF]."?lang=ja"; ?>"><img src="ja_flag.gif" border=0></a>
18: </p>
19: </BODY>
20: </HTML>
Looking at Listing 26.3, you'll notice it's a very basic template because all the language-
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 26.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 26.1. Figure 26.1. Viewing the language selector for the first time.
Until another language is selected, the default is English;
Figure 26.2. Viewing the German language page.
Figure 26.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
Companies that offer localized versions of their Web sites often have long discussions about how to represent the locale selectionsflags, names of countries, names of languages, and so forth. There is no
|