The process of making your applications/websites usable in many different locales is called internationalization, While customizing your code for different locales is called localization. Localization is the process of making your applications or websites local to where it is being viewed. For example, you can make a website more local to a particular place by converting its text to the predominate language of that location and by displaying the local time (e.g. German for people living in Germany or French for people living in France).
Yahoo and Google have examples of localized sites: Google.com (US), Google.co.uk (UK), Yahoo.jp (Japan) and so forth. Creating a Web site that is useful to speakers of different languages is a breeze using PHP. In this article, we look at how to create a localized welcome page that uses PHP to enable a user to select a target language and then receive the appropriate text. An example of externalizing the strings used in this script -- one of the characteristics of internationalization -- will also be provided.
The Structure of a Localized Web Page
In this example, the user will choose one of three languages and the website will then display a welcome message in the chosen language. Because we want the welcome page to be locale specific, we need to find a way to send information to the welcome page that will tell it what language to use and how to render the text. Thus, we need three things:
- A file that will contain all the locale-specific data for each locale
- A file that will help in rendering the appropriate locale
- A main page that will display the text
include()function to make use of them. When a user comes to the welcome page, it initially will be in English and then it will present the option to select one of the other languages.
PHP Script with Locale-specific Data
Let's begin by creating the file (lang.php) that will contain the locale-specific information.
The purpose of this script is to set the preferred language that the user has chosen so that it can be used when the welcome page is loaded. The code starts by checking which language the person selected:
If no locale has been selected, the
$currlang variable is set to English, which has an
en language code. Otherwise, the
$currlang value is set to whatever language the user selected. If your site were Japanese by default, you would change the file to reflect this by using the Japanese locale instead.
We will not be using the
session_start() function in this script, because the script itself will be included in the welcome page and it will be valid for any scripts included after it has been declared.
PHP Script to Render the Appropriate Locale
The next section of this script contains a
switch() function, which will match the selected language code to a character set and a language code and then send the headers.
When the appropriate character set and language code has been set, it is then assigned to the
LANGCODE constant variables, which will be used by the main page. 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. That's all there is to this script.
The Welcome Message Text
Next, we need to set the welcome message that will be displayed on the welcome page. Here's the script (txt.php) that will do just that:
The language depends on what is contained in the
$_SESSION['lang'] session variable. We use the
switch() function to evaluate and check what language has been selected. The
defineString() function will match whatever language the user chooses to the appropriate text. As an example, this is the code used when the user chooses English as the preferred language:
Note that the default language is English, as shown in the
WELCOME_TXT constant variable contains the text that welcomes a user to the site, which can be in any of the available languages. The
CHOOSE_TXT constant variable contains the text that introduces the language selection process.