Skip to content

Commit ce3b972

Browse files
committed
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
1 parent b2a62ae commit ce3b972

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/I18n/Formatter/IcuFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function format($locale, $message, array $vars)
6565
$count = isset($vars['_count']) ? $vars['_count'] : 0;
6666
unset($vars['_count'], $vars['_singular']);
6767
$form = PluralRules::calculate($locale, $count);
68-
$message = $message[$form];
68+
$message = isset($message[$form]) ? $message[$form] : end($message);
6969
}
7070

7171
return $this->_formatMessage($locale, $message, $vars);

tests/TestCase/I18n/TimeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Cake\Test\TestCase\I18n;
1616

1717
use Cake\I18n\FrozenTime;
18+
use Cake\I18n\I18n;
1819
use Cake\I18n\Time;
1920
use Cake\TestSuite\TestCase;
2021

@@ -55,6 +56,7 @@ public function tearDown()
5556
FrozenTime::$defaultLocale = $this->locale;
5657
FrozenTime::resetToStringFormat();
5758
date_default_timezone_set('UTC');
59+
I18n::locale(I18n::DEFAULT_LOCALE);
5860
}
5961

6062
/**
@@ -807,6 +809,20 @@ public function testParseTime($class)
807809
$this->assertNull($time);
808810
}
809811

812+
/**
813+
* Tests that timeAgoInWords when using a russian locale does not break things
814+
*
815+
* @dataProvider classNameProvider
816+
* @return void
817+
*/
818+
public function testRussianTimeAgoInWords($class)
819+
{
820+
I18n::locale('ru_RU');
821+
$time = new $class('5 days ago');
822+
$result = $time->timeAgoInWords();
823+
$this->assertEquals('5 days ago', $result);
824+
}
825+
810826
/**
811827
* Tests that parsing a date respects de default timezone in PHP.
812828
*

0 commit comments

Comments
 (0)