Skip to content

Commit

Permalink
Add fallback fparameter to language has function so we can truly chec…
Browse files Browse the repository at this point in the history
…k if the translation exists
  • Loading branch information
bobbybouwmann committed Oct 30, 2015
1 parent 2ae928a commit 200cff2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ public function __construct(LoaderInterface $loader, $locale)
*
* @param string $key
* @param string $locale
* @param bool $fallback
* @return bool
*/
public function has($key, $locale = null)
public function has($key, $locale = null, $fallback = true)
{
return $this->get($key, [], $locale) !== $key;
return $this->get($key, [], $locale, $fallback) !== $key;
}

/**
Expand All @@ -69,16 +70,19 @@ public function has($key, $locale = null)
* @param string $key
* @param array $replace
* @param string $locale
* @param bool $fallback
* @return string
*/
public function get($key, array $replace = [], $locale = null)
public function get($key, array $replace = [], $locale = null, $fallback = true)
{
list($namespace, $group, $item) = $this->parseKey($key);

// Here we will get the locale that should be used for the language line. If one
// was not passed, we will use the default locales which was given to us when
// the translator was instantiated. Then, we can load the lines and return.
foreach ($this->parseLocale($locale) as $locale) {
$locales = $fallback ? $this->parseLocale($locale) : [$locale];

foreach ($locales as $locale) {
$this->load($namespace, $group, $locale);

$line = $this->getLine(
Expand Down
8 changes: 8 additions & 0 deletions tests/Translation/TranslationTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public function testHasMethodReturnsFalseWhenReturnedTranslationIsNull()
$t = $this->getMock('Illuminate\Translation\Translator', ['get'], [$this->getLoader(), 'en', 'sp']);
$t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('bar'))->will($this->returnValue('bar'));
$this->assertTrue($t->has('foo', 'bar'));

$t = $this->getMock('Illuminate\Translation\Translator', ['get'], [$this->getLoader(), 'en']);
$t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('bar'), false)->will($this->returnValue('bar'));
$this->assertTrue($t->has('foo', 'bar', false));

$t = $this->getMock('Illuminate\Translation\Translator', ['get'], [$this->getLoader(), 'en']);
$t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('bar'), false)->will($this->returnValue('foo'));
$this->assertFalse($t->has('foo', 'bar', false));
}

public function testGetMethodProperlyLoadsAndRetrievesItem()
Expand Down

0 comments on commit 200cff2

Please sign in to comment.