Permalink
Browse files

`Message::translate()` to make count absolute before passing.

  • Loading branch information...
1 parent 973235a commit 158a7e1cccbf16533189098f276910d4af087e30 @davidpersson davidpersson committed with gwoo Feb 25, 2010
Showing with 62 additions and 2 deletions.
  1. +8 −2 libraries/lithium/g11n/Message.php
  2. +54 −0 libraries/lithium/tests/cases/g11n/MessageTest.php
@@ -71,7 +71,13 @@ class Message extends \lithium\core\StaticObject {
* @see lithium\util\String::insert()
* @param string $id The id to use when looking up the translation.
* @param array $options Valid options are:
- * - `'count'`: Used to determine the correct plural form.
+ * - `'count'`: Used to determine the correct plural form. You can either pass
+ * a signed or unsigned integer, the behavior when passing other types
+ * is yet undefined.
+ * The count is made absolute before being passed to the pluralization
+ * function. This has the effect that that with i.e. an English
+ * pluralization function passing `-1` results in a singular
+ * translation.
* - `'locale'`: The target locale, defaults to current locale.
* - `'scope'`: The scope of the message.
* - `'default'`: Is used as a fall back if `_translated()` returns
@@ -93,7 +99,7 @@ public static function translate($id, array $options = array()) {
if ($noop) {
$result = null;
} else {
- $result = static::_translated($id, $count, $locale, compact('scope'));
+ $result = static::_translated($id, abs($count), $locale, compact('scope'));
}
if ($result || $default) {
@@ -64,6 +64,60 @@ public function testTranslatePlural() {
$this->assertEqual($expected, $result);
}
+ public function testTranslateNonIntegerCounts() {
+ $data = array(
+ 'house' => array('Haus', 'Häuser')
+ );
+ Catalog::write('message', 'de', $data, array('name' => 'runtime'));
+
+ $expected = 'Häuser';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => 2.31));
+ $this->assertEqual($expected, $result);
+
+ $expected = 'Häuser';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => 1.1));
+ $this->assertEqual($expected, $result);
+
+ $expected = 'Häuser';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => 0.1));
+ $this->assertEqual($expected, $result);
+
+ $expected = 'Haus';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => true));
+ $this->assertEqual($expected, $result);
+
+ $expected = 'Häuser';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => false));
+ $this->assertEqual($expected, $result);
+
+ $expected = 'Häuser';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => '2'));
+ $this->assertEqual($expected, $result);
+
+ $expected = 'Häuser';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => '0'));
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testTranslateNegativeIntegerCounts() {
+ $data = array(
+ 'house' => array('Haus', 'Häuser')
+ );
+ Catalog::write('message', 'de', $data, array('name' => 'runtime'));
+
+ $expected = 'Haus';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => -1));
+ $this->assertEqual($expected, $result);
+
+ $expected = 'Häuser';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => -2));
+ $this->assertEqual($expected, $result);
+
+ $expected = 'Häuser';
+ $result = Message::translate('house', array('locale' => 'de', 'count' => -5));
+ $this->assertEqual($expected, $result);
+ }
+
public function testTranslateFail() {
$result = Message::translate('catalog', array('locale' => 'de'));
$this->assertNull($result);

0 comments on commit 158a7e1

Please sign in to comment.