CLDR plural parser in PHP
authorTim Starling <tstarling@wikimedia.org>
Thu, 16 Aug 2012 10:36:08 +0000 (20:36 +1000)
committerNiklas Laxström <niklas.laxstrom@gmail.com>
Wed, 22 Aug 2012 12:55:50 +0000 (12:55 +0000)
commitfceb9bcb49fdeaaf978a1667ef382aedb6e715d1
tree6bd3cc76bd63fa05053dbc23216fdb11fceb788e
parentbbbcf089dbbb84dcd92e2332d2cf7222d7140647
CLDR plural parser in PHP

Wrote a CLDR plural rule parser to replace the eval()-based one from
I58a9cdfe. It converts the infix notation of the XML files to a
sanitized RPN notation, referred to in external interfaces as the
"compiled" form. The RPN notation is cached and then executed by a
fast non-validating evaluator.

Timings for the largest rule in the XML file are ~1.2ms for
compilation and ~200us for execution.

Also:
* Lazy-load the plural rules when recache() requests them, instead of
  loading them for every request.
* Language::convertPlural() needs integer keys, and CLDR only gives
  string keys. The previous code was not mapping them so it didn't work
  at all. I just mapped them in the order they appear in the XML file,
  i.e. the first rule becomes MediaWiki's $pluralForm=0, the second
  becomes $pluralForm=1, etc. Not sure if there is a more rigorous way
  to do it.

Change-Id: I65ee788c1a8e5ee2ede2091990d86eb722749dd3
includes/LocalisationCache.php
languages/Language.php
languages/utils/CLDRPluralRuleEvaluator.php
tests/phpunit/languages/utils/CLDRPluralRuleEvaluatorTest.php [new file with mode: 0644]