From ce3b97201dc9c6682bf362685a7f5768c96aa052 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 3 Mar 2016 23:12:22 +0100 Subject: [PATCH] Avoding errors when there is no translation for the given plural form While this avoids an annoying notice, it also hides problems in the translation files. A more propper solution would be to use the ICU message selector syntax instead of the magic __n() function, but it would require breaking the translation files that already exist for the time class. Please enter the commit message for your changes. Lines starting --- src/I18n/Formatter/IcuFormatter.php | 2 +- tests/TestCase/I18n/TimeTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/I18n/Formatter/IcuFormatter.php b/src/I18n/Formatter/IcuFormatter.php index d0084dd8976..6090a370642 100644 --- a/src/I18n/Formatter/IcuFormatter.php +++ b/src/I18n/Formatter/IcuFormatter.php @@ -65,7 +65,7 @@ public function format($locale, $message, array $vars) $count = isset($vars['_count']) ? $vars['_count'] : 0; unset($vars['_count'], $vars['_singular']); $form = PluralRules::calculate($locale, $count); - $message = $message[$form]; + $message = isset($message[$form]) ? $message[$form] : end($message); } return $this->_formatMessage($locale, $message, $vars); diff --git a/tests/TestCase/I18n/TimeTest.php b/tests/TestCase/I18n/TimeTest.php index 368c723090f..28299fea4f4 100644 --- a/tests/TestCase/I18n/TimeTest.php +++ b/tests/TestCase/I18n/TimeTest.php @@ -15,6 +15,7 @@ namespace Cake\Test\TestCase\I18n; use Cake\I18n\FrozenTime; +use Cake\I18n\I18n; use Cake\I18n\Time; use Cake\TestSuite\TestCase; @@ -55,6 +56,7 @@ public function tearDown() FrozenTime::$defaultLocale = $this->locale; FrozenTime::resetToStringFormat(); date_default_timezone_set('UTC'); + I18n::locale(I18n::DEFAULT_LOCALE); } /** @@ -807,6 +809,20 @@ public function testParseTime($class) $this->assertNull($time); } + /** + * Tests that timeAgoInWords when using a russian locale does not break things + * + * @dataProvider classNameProvider + * @return void + */ + public function testRussianTimeAgoInWords($class) + { + I18n::locale('ru_RU'); + $time = new $class('5 days ago'); + $result = $time->timeAgoInWords(); + $this->assertEquals('5 days ago', $result); + } + /** * Tests that parsing a date respects de default timezone in PHP. *