In traditional software applications it is a good practice to externalize all text for labels and messages that are displayed to the user. This basic part of internationalization provides a mechanism to translate software without having to change the code base. It also assists the translation team by not requiring them to dig through code to extract the text. On the other hand, it may not be necessary for PHP applications that are basically HTML content with additional capabilities added on, such as personalization and search, for an improved user experience. This section will describe how to externalize display text from PHP code and re-inject it at run time for the desired locale. Previous sections discussed how to find user preferences for language and regional settings.
The most basic strategy here is to externalize display text to an array and use a function to look it up for a given locale. Here is an example.
The function lookupMessage($locale, $key) defines two arrays to act as resource bundles for each
of the locales
en_US and zh_CN. Based on the locale and a key supplied by the calling code the appropriate
message is looked up and returned. Such a simple approach may be justified on web sites with light internationalization
requirements but there are a few problems. Firstly, some messages, such as the second message in the array, may
require parameters. The different grammatical structures of languages prevents this from being done with string
concatenation but using the sprintf() function to embed parameters in a message is an option. The
second problem is that the externalized text has not been fully externalized but is, actually, still in the PHP code.
We don't want our translators to have to look at PHP code and we don't want to retest every time we change a label so
display text is best left out of PHP code.
Thirdly, if one language is mostly similar to another, such as British and American English, then we want a mechanism
to only include messages from those that differ in the base language to avoid the maintenance associated with cut and paste.
Given that these problems are not that hard to solve there is a better approach than inventing your own solution.
The gettext() function is an established message catalog framework to avoid the problems with the simple
scheme just discussed. This is discussed in the PHP Manual and on the GNU gettext web site
[GNU].
If you are serious about internationalizing your application then you will not put any text in your images. This will be a nightmare for your translators and will substantially increase your development costs.
About 关于本网站 © chinesenotes.com 2007-2010. Please send comments to alex@chinesenotes.com.