Take a look at how the Mantis BugTracking software handles internationalization. The method that they use is rather nice.
Additional Information:
It has been years since I have used it, but a quick look through the source code shows that this portion of the code has not changed significantly. They use the common message catalog and get message approach that many products use. Their language API is pretty simple - the phpxref output is available and it isn't that surprising. The message catalog is implemented as a PHP script that simply gets include
'd. For example, the catalog for English contains entries like:
$s_new_bug = 'New Issue';
$s_bugnote_added = 'Note Added';
It contains about 1,600 or so declarations. The interesting magic happens inside of lang_load
. When a language is loaded, the catalog file is included so all of the variables that it defines are defined in the local scope. Lang_load
iterates over the locally defined variables and builds a message map based on the variable names so that it can look up the message by name. For example, after loading the previous snippet, it will be as if the following statements were executed:
$g_lang_strings['en']['new_bug'] = 'New Issue';
$g_lang_strings['en']['bugnote_added'] = 'Note Added';
When the UI needs to access a "hard coded" string, it uses a call like lang_get('new_bug')
which will:
- Lookup the preferred language in the settings for the current user
- Ensure that the language map is loaded by calling
lang_load()
- Return the value from the appropriate language map
The interesting thing is that all of the machinery is lazily loaded. You don't pay for the fact that they have 50 or so languages defined until you need to access one of them. Overall, this is probably one of the most inpressive PHP applications that I have dug into over the years.