Skip to content

Commit

Permalink
[9.x] Fixes unable to use trans()->has() on JSON language files. (#…
Browse files Browse the repository at this point in the history
…47582)

* [9.x] Fixes unable to use `tran()->has()` on JSON language files.

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Jun 27, 2023
1 parent df9100e commit a6ed3e6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,18 @@ public function hasForLocale($key, $locale = null)
*/
public function has($key, $locale = null, $fallback = true)
{
return $this->get($key, [], $locale, $fallback) !== $key;
$locale = $locale ?: $this->locale;

$line = $this->get($key, [], $locale, $fallback);

// For JSON translations, the loaded files will contain the correct line.
// Otherwise, we must assume we are handling typical translation file
// and check if the returned line is not the same as the given key.
if (! is_null($this->loaded['*']['*'][$locale][$key] ?? null)) {
return true;
}

return $line !== $key;
}

/**
Expand Down
43 changes: 43 additions & 0 deletions tests/Integration/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Illuminate\Tests\Integration\Translation;

use Orchestra\Testbench\TestCase;

class TranslatorTest extends TestCase
{
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function defineEnvironment($app)
{
$app['translator']->addJsonPath(__DIR__.'/lang');

parent::defineEnvironment($app);
}

public function testItCanGetFromLocaleForJson()
{
$this->assertSame('30 Days', $this->app['translator']->get('30 Days'));

$this->app->setLocale('fr');

$this->assertSame('30 jours', $this->app['translator']->get('30 Days'));
}

public function testItCanCheckLanguageExistsHasFromLocaleForJson()
{
$this->assertTrue($this->app['translator']->has('1 Day'));
$this->assertTrue($this->app['translator']->hasForLocale('1 Day'));
$this->assertTrue($this->app['translator']->hasForLocale('30 Days'));

$this->app->setLocale('fr');

$this->assertFalse($this->app['translator']->has('1 Day'));
$this->assertFalse($this->app['translator']->hasForLocale('1 Day'));
$this->assertTrue($this->app['translator']->hasForLocale('30 Days'));
}
}
7 changes: 7 additions & 0 deletions tests/Integration/Translation/lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"1 Day": "1 Day",
"30 Days": "30 Days",
"365 Days": "365 Days",
"60 Days": "60 Days",
"90 Days": "90 Days"
}
6 changes: 6 additions & 0 deletions tests/Integration/Translation/lang/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"30 Days": "30 jours",
"365 Days": "365 jours",
"60 Days": "60 jours",
"90 Days": "90 jours"
}

0 comments on commit a6ed3e6

Please sign in to comment.