r57305 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57304‎ | r57305 | r57306 >
Date:19:54, 2 October 2009
Author:dale
Status:deferred
Tags:
Comment:
* clrd class stubs to add plural support for mwEmbed
Modified paths:
  • /trunk/phase3/js2/mwEmbed/php/clrdConverter.php (added) (history)

Diff [purge]

Index: trunk/phase3/js2/mwEmbed/php/clrdConverter.php
@@ -0,0 +1,207 @@
 2+<?php
 3+/*
 4+ * cldr format tries to build a structured representation of
 5+ * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
 6+ *
 7+ * it has structure similar to
 8+ * http://www.unicode.org/repos/cldr/tags/release-1-7-1/common/supplemental/plurals.xml
 9+ *
 10+ * It works by mapping array indexes to forms passed in via the PLURAL call
 11+ * the plural types are linear mapped to PLURAL argument index.
 12+ *
 13+ * Rules should come in the following order:
 14+ * zero, one, two, few, many, other (other is a special default case that applies to all)
 15+ *
 16+ * clrd also allows export of rule sets in json to associated javascript intepretation
 17+ */
 18+
 19+class clrdConverter {
 20+ static $masterCLDR = array(
 21+ // if locale is known to have no plurals, there are no rules
 22+ array(
 23+ 'locales'=> array('az','fa','hu','ja','ko','my to','tr','vi','yo','zh',
 24+ 'bo','dz','id','jv ka','km','kn','ms','th')
 25+ ),
 26+ array(
 27+ 'locales'=> array('ar'),
 28+ 'rules' => array(
 29+ 'zero' => 0,
 30+ 'one' => 1,
 31+ 'two' => 2,
 32+ //n mod 100 in 3..10
 33+ 'few' => array( 'mod' => 100, 'is'=>'3-10' ),
 34+ //n mod 100 in 11..99
 35+ 'many' => array( 'mod' => 100, 'is'=>'11-99')
 36+ )
 37+ ),
 38+ array( 'locales' => array( 'da','de','el','en','eo','es','et','fi','fo','gl',
 39+ 'he','iw','it','nb','nl','nn','no','pt_PT','sv',
 40+ 'af','bg','bn','ca','eu','fur','fy','gu','ha',
 41+ 'is','ku','lb','ml','mr','nah','ne','om','or',
 42+ 'pa','pap','ps','so','sq','sw','ta','te','tk',
 43+ 'ur','zu','mn','gsw'),
 44+ 'rules' => array(
 45+ 'one' => 1
 46+ )
 47+ ),
 48+ array( 'locales' => array('pt','am','bh','fil','tl','guw','hi','ln','mg','nso','ti','wa'),
 49+ 'rules'=> array(
 50+ 'one'=> '0-1'
 51+ )
 52+ ),
 53+ array( 'locales' => array('fr'),
 54+ 'rules'=>array(
 55+ //n within 0..2 and n is not 2
 56+ 'one' => array( 'is'=>'0-2', 'not' => 2)
 57+ )
 58+ ),
 59+ array( 'locales' => array('lv'),
 60+ 'rules' => array(
 61+ 'zero' => 0,
 62+ //n mod 10 is 1 and n mod 100 is not 11
 63+ 'one'=>array(
 64+ array( 'mod' => 10, 'is' => 1 ),
 65+ //AND
 66+ array( 'mod' => 100, 'not' => 11)
 67+ )
 68+ )
 69+ ),
 70+ array( 'locales' => array('ga','se','sma','smi','smj','smn','sms'),
 71+ 'rules' => array(
 72+ 'one' => 1,
 73+ 'two' => 2
 74+ )
 75+ ),
 76+ array( 'locales' => array('ro','mo'),
 77+ 'rules' => array(
 78+ 'one' => 1,
 79+ //n is 0 OR n is not 1 AND n mod 100 in 1..19
 80+ 'few' => array(
 81+ array( 'is' => 0),
 82+ 'or',
 83+ array(
 84+ array( 'not' => 1),
 85+ array( 'mod' => 100, 'is'=>'1-19')
 86+ )
 87+ )
 88+ )
 89+ ),
 90+ array( 'locales' => array( 'lt' ),
 91+ 'rules' => array(
 92+ //n mod 10 is 1 and n mod 100 not in 11..19
 93+ 'one' => array(
 94+ array( 'mod'=>10, 'is'=> 1 ),
 95+ array( 'mod'=> 100, 'not'=> '11-19')
 96+ ),
 97+ //n mod 10 in 2..9 and n mod 100 not in 11..19
 98+ 'few' => array(
 99+ array( 'mod'=> 10, 'is'=> '2-9' ),
 100+ array( 'mod'=> 100, 'not' => '11-19')
 101+ ),
 102+ )
 103+ ),
 104+ array( 'locales' => array( 'hr','ru','sr','uk','be','bs','sh' ),
 105+ 'rules' => array(
 106+ //n mod 10 is 1 and n mod 100 is not 11
 107+ 'one' => array(
 108+ array( 'mod' => 10, 'is' => 1),
 109+ array( 'mod' => 100, 'not' => 11)
 110+ ),
 111+ //n mod 10 in 2..4 and n mod 100 not in 12..14
 112+ 'few' => array(
 113+ array( 'mod' => 10, 'is' => '2-4'),
 114+ array( 'mod' => 100, 'not' => '12-14')
 115+ ),
 116+ //n mod 10 is 0 or n mod 10 in 5..9 or n mod 100 in 11..14
 117+ 'many' => array(
 118+ array( 'mod'=> 10, 'is' => 0),
 119+ 'or',
 120+ array( 'mod'=> 10, 'is' => '5-9'),
 121+ 'or',
 122+ array( 'mod'=> 100, 'is' => '11-14')
 123+ ),
 124+ )
 125+ ),
 126+ array( 'locales' => array('cs','sk'),
 127+ 'rules' => array(
 128+ 'one' => 1,
 129+ 'few'=> '2-4'
 130+ )
 131+ ),
 132+ array( 'locales' => array('pl'),
 133+ 'rules '=> array(
 134+ 'one' => 1,
 135+ 'few' => array(
 136+ //n mod 10 in 2..4
 137+ array( 'mod' => 10, 'is' => '2-4'),
 138+ //and n mod 100 not in 12..14
 139+ array( 'mod' => 100, 'not'=> '12-14'),
 140+ //and n mod 100 not in 22..24
 141+ array( 'mod' => 100, 'in' => '22-24')
 142+ )
 143+ )
 144+ ),
 145+ array( 'locales' => array('sl'),
 146+ 'rules' => array(
 147+ 'one' => array( 'mod'=>100, 'is' => 1 ),
 148+ 'two' => array( 'mod'=>100, 'is' => 2 ),
 149+ 'few' => array( 'mod'=>100, 'is' => '3-4')
 150+ )
 151+ ),
 152+ array( 'locales' => array('mt'),
 153+ 'rules' => array(
 154+ 'one' => 1,
 155+ //n is 0 or n mod 100 in 2..10
 156+ 'few' => array(
 157+ array( 'is' => 0 ),
 158+ 'or',
 159+ array( 'mod' => 100, 'is' => '2-10')
 160+ ),
 161+ //n mod 100 in 11..19
 162+ 'many' => array( 'mod'=>100, 'is' => '11-19')
 163+ )
 164+ ),
 165+ array( 'locales' => array( 'mk' ),
 166+ 'rules' => array(
 167+ 'one' => array('mod' => 10, 'is' => '1')
 168+ )
 169+ ),
 170+ array( 'locales' => array( 'cy' ),
 171+ 'rules' => array(
 172+ 'one' => 1,
 173+ 'two' => 2,
 174+ //n is 8 or n is 11
 175+ 'many' => array(
 176+ array( 'is' => 8 ),
 177+ array( 'is' => 11 )
 178+ )
 179+ )
 180+ )
 181+ );
 182+ //takes the cldr representation and returns the proper form
 183+ function cldrConvertPlural($count, $forms){
 184+ if ( !count($forms) ) { return ''; }
 185+ //get the rule set
 186+ $ruleSet = $this->getCldrRuleSet();
 187+ //get the number of forms (ruleSet Count + 1 for 'other' )
 188+ $fomsCount = count( $ruleSet ) + 1;
 189+
 190+ //if count is 1 no plurals for this language:
 191+ if( count( $forms ) == 1)
 192+ return $forms[0];
 193+
 194+ $forms = $this->preConvertPlural( $forms, $fomsCount );
 195+
 196+ }
 197+
 198+ function getCldrRuleSet(){
 199+ $code = $this->getCode();
 200+ foreach($this->masterCLDR as $ruleSet){
 201+ if( in_array($code, $ruleSet['locales']) ){
 202+ return $ruleSet['rules'];
 203+ }
 204+ }
 205+ //could not find the language code
 206+ return false;
 207+ }
 208+}
\ No newline at end of file

Status & tagging log