Pressure is nothing more than the shadow of great opportunity. - Michael Johnson

Translation Library Updated

9 years, 8 months ago

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.

  1. <?php
  2. $oTranslation->Get('results.paging', array(
  3. 'pageNum' => $iPageNum,
  4. 'numPages' => $iNumPages
  5. ));
  6. ?>

The corresponding translation key/value pair might look like this:

  1. 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).

  1. {inherits en}

You might use this functionality to cater for the differences between, for example, British English and American English.

Download

» Download Library

Credits

Thanks to Brad and Mike for their input.

Comments

No comments written for this post yet. Please check back soon or consider writing the first one below!

Help make this post better

Notes: Standard BBCode for links, bold, italic and code are supported. rel="nofollow" is added to all links. Your email address will never be displayed on the site.

Back to index