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:
Listing 27.1 shows the contents of the master file used for sending locale-specific header information.
Listing 27.1. Language Definition File
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
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
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.