I've updated the translation library I wrote about in Managing Translation Strings to include:
- Improved parsing of translation key/value pairs - simplified to a single regular expression. This also means it's no longer necessary to escape literal equals (=) in translation key values although it is still necessary for literal hashes (#).
- Named substitution variables (as well as numeric).
- Inheritance of translation files.
Named Substitution Variables
You can now pass an associative array as the second parameter to the "Get" method which contains key/value pairs. This enables you to provide more context for people providing translations but will make your templates slightly more verbose.
The following code shows how to reference a translation with named substitution variables.
<?php$oTranslation->Get('results.paging', array('pageNum' => $iPageNum,'numPages' => $iNumPages));?>
The corresponding translation key/value pair might look like this:
results.paging = Displaying page {pageNum} of {numPages}.
Inheritance of Translation Files
The library now supports an "inherits" directive to specify that a translation file inherits from another. Redefined keys override those present in the parent. The "inherits" directive must appear on the first line of the file and cannot be proceeded by comments. It takes a single parameter which contains the name of the file it inherits from (excluding the file extension).
{inherits en}
You might use this functionality to cater for the differences between, for example, British English and American English.