function test_english() { $choice = new ChoiceFormat(); $string = '[0] none |{n: n % 10 == 1} 1st |{n: n % 10 == 2} 2nd |{n: n % 10 == 3} 3rd |{n:n} th'; $wants = array('none' => array(0), '1st' => array(1, 11, 21), '2nd' => array(2, 12, 22), '3rd' => array(3, 13, 23), 'th' => array(4, 5, 6, 7, 14, 15)); foreach ($wants as $want => $numbers) { foreach ($numbers as $n) { $this->assertEquals($want, $choice->format($string, $n)); } } }
/** * Constructor * * @param string f default NULL format string */ public function __construct($f = NULL) { // Add some default formatters $this->setFormatter('printf', PrintfFormat::getInstance()); $this->setFormatter('date', DateFormat::getInstance()); $this->setFormatter('choice', ChoiceFormat::getInstance()); $this->setFormatter('number', NumberFormat::getInstance()); $this->setFormatter('array', ArrayFormat::getInstance()); $this->setFormatter('hash', HashFormat::getInstance()); parent::__construct($f); }
function testChoices() { $choice = new ChoiceFormat(); $string = '[0] are no files |[1] is one file |(1,Inf] are {number} files'; $want = 'are no files'; $this->assertEqual($want, $choice->format($string, 0)); $want = 'is one file'; $this->assertEqual($want, $choice->format($string, 1)); $want = 'are {number} files'; $this->assertEqual($want, $choice->format($string, 5)); $this->assertFalse($choice->format($string, -1)); $string = '{1,2} one two |{3,4} three four |[2,5] two to five inclusive'; $this->assertEqual($choice->format($string, 1), 'one two'); $this->assertEqual($choice->format($string, 2.1), 'two to five inclusive'); $this->assertEqual($choice->format($string, 3), 'three four'); }
/** * Display the choosen translated string. * Overrides the parent method, also calls parent's renderBody to * translate. */ protected function renderBody() { $choice = new ChoiceFormat(); //call the parent to translate it first. $text = parent::renderBody(); //trim it if ($this->doTrim()) { $text = trim($text); } $value = $this->getValue(); $string = $choice->format($text, $value); if ($string) { return strtr($string, array('{Value}' => $value)); } }
/** * Display the choosen translated string. * Overrides the parent method, also calls parent's renderBody to * translate. */ protected function translateText($text, $subs) { $text = parent::translateText($text, $subs); $choice = new ChoiceFormat(); $value = $this->getValue(); $string = $choice->format($text, $value); if ($string) { return strtr($string, array('{Value}' => $value)); } }