diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index 2a956830df98..c8ebaa4e73c0 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -191,7 +191,7 @@ public function get($key, array $replace = [], $locale = null, $fallback = true) public function choice($key, $number, array $replace = [], $locale = null) { $line = $this->get( - $key, $replace, $locale = $this->localeForChoice($locale) + $key, $replace, $locale = $this->localeForChoice($key, $locale) ); // If the given "number" is actually an array or countable we will simply count the @@ -211,12 +211,15 @@ public function choice($key, $number, array $replace = [], $locale = null) /** * Get the proper locale for a choice operation. * + * @param string $key * @param string|null $locale * @return string */ - protected function localeForChoice($locale) + protected function localeForChoice($key, $locale) { - return $locale ?: $this->locale ?: $this->fallback; + $locale = $locale ?: $this->locale; + + return $this->hasForLocale($key, $locale) ? $locale : $this->fallback; } /** diff --git a/tests/Translation/TranslationTranslatorTest.php b/tests/Translation/TranslationTranslatorTest.php index 86c8948ef94f..f160b3ae1df9 100755 --- a/tests/Translation/TranslationTranslatorTest.php +++ b/tests/Translation/TranslationTranslatorTest.php @@ -125,8 +125,9 @@ public function testGetMethodProperlyLoadsAndRetrievesItemForGlobalNamespace() public function testChoiceMethodProperlyLoadsAndRetrievesItemForAnInt() { - $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock(); + $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get', 'localeForChoice'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock(); $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo(['replace']), $this->equalTo('en'))->willReturn('line'); + $t->expects($this->once())->method('localeForChoice')->with($this->equalTo('foo'), $this->equalTo(null))->willReturn('en'); $t->setSelector($selector = m::mock(MessageSelector::class)); $selector->shouldReceive('choose')->once()->with('line', 10, 'en')->andReturn('choiced'); @@ -135,8 +136,9 @@ public function testChoiceMethodProperlyLoadsAndRetrievesItemForAnInt() public function testChoiceMethodProperlyLoadsAndRetrievesItemForAFloat() { - $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock(); + $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get', 'localeForChoice'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock(); $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo(['replace']), $this->equalTo('en'))->willReturn('line'); + $t->expects($this->once())->method('localeForChoice')->with($this->equalTo('foo'), $this->equalTo(null))->willReturn('en'); $t->setSelector($selector = m::mock(MessageSelector::class)); $selector->shouldReceive('choose')->once()->with('line', 1.2, 'en')->andReturn('choiced'); @@ -145,8 +147,9 @@ public function testChoiceMethodProperlyLoadsAndRetrievesItemForAFloat() public function testChoiceMethodProperlyCountsCollectionsAndLoadsAndRetrievesItem() { - $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock(); + $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get', 'localeForChoice'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock(); $t->expects($this->exactly(2))->method('get')->with($this->equalTo('foo'), $this->equalTo(['replace']), $this->equalTo('en'))->willReturn('line'); + $t->expects($this->exactly(2))->method('localeForChoice')->with($this->equalTo('foo'), $this->equalTo(null))->willReturn('en'); $t->setSelector($selector = m::mock(MessageSelector::class)); $selector->shouldReceive('choose')->twice()->with('line', 3, 'en')->andReturn('choiced'); @@ -157,6 +160,18 @@ public function testChoiceMethodProperlyCountsCollectionsAndLoadsAndRetrievesIte $t->choice('foo', $values, ['replace']); } + public function testChoiceMethodProperlySelectsLocaleForChoose() + { + $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get', 'hasForLocale'])->setConstructorArgs([$this->getLoader(), 'cs'])->getMock(); + $t->setFallback('en'); + $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo(['replace']), $this->equalTo('en'))->willReturn('line'); + $t->expects($this->once())->method('hasForLocale')->with($this->equalTo('foo'), $this->equalTo('cs'))->willReturn(false); + $t->setSelector($selector = m::mock(MessageSelector::class)); + $selector->shouldReceive('choose')->once()->with('line', 10, 'en')->andReturn('choiced'); + + $t->choice('foo', 10, ['replace']); + } + public function testGetJson() { $t = new Translator($this->getLoader(), 'en');