Create a Localized Web Page with PHP [con't]
The Main Page to Display Welcome Message
The next thing we do is create the main page. The main page (index.php) will include the lang.php and txt.php files.
One of the key elements of internationalization is the externalization of all strings, so only one master file needs to be used. The index page above is such a file. So let's walk through the code step by step.
The first part of the code includes both the lang.php and txt.php files, which include all the language and character set information:
session_start()function is used to initialize all the session variables in the included files and the index file. The
defineStrings()variable has also been called.
Next, the actual HTML page is started.
Now, the very first PHP code retrieves the welcome text and displays it.
Then the meta-tags are used to define the character set that should be used.
If you remember, the character set is defined in the
lang.php file, where the chosen language is matched with the appropriate character set.
Next, the language code is set.
The actual language code is set in the
lang.php file and retrieved by the main page. Like the character set variable, it changes based on the user's preference. Now in the body of the main page, all the flags of the countries are shown with the language code clearly displayed:
$_SERVER[PHP_SELF] variable ensures that when the user clicks on a link, it reloads the index page and the HTML links send a language variable that contains en, de or ja language codes to the index page. When the user clicks on one of the flags, the locale will change to the new selected locale, and the strings used will be appropriate to the new locale. These links contain the
lang variable, which is passed to the script as
$_GET[lang]. So, basically all that the index.php file does is to display a welcome message, based on the selected language preference.
As you can see, localization is not merely translation; translation is only a part of localization. Localization can be a useful option for those who speak only one language, and if you are running an online business localization provides access to people from other countries who potentially can be buyers of your product.
Original: Oct. 20, 2010