Skip to content

Commit a6ed3e6

Browse files
authored
[9.x] Fixes unable to use trans()->has() on JSON language files. (#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]>
1 parent df9100e commit a6ed3e6

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

src/Illuminate/Translation/Translator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,18 @@ public function hasForLocale($key, $locale = null)
101101
*/
102102
public function has($key, $locale = null, $fallback = true)
103103
{
104-
return $this->get($key, [], $locale, $fallback) !== $key;
104+
$locale = $locale ?: $this->locale;
105+
106+
$line = $this->get($key, [], $locale, $fallback);
107+
108+
// For JSON translations, the loaded files will contain the correct line.
109+
// Otherwise, we must assume we are handling typical translation file
110+
// and check if the returned line is not the same as the given key.
111+
if (! is_null($this->loaded['*']['*'][$locale][$key] ?? null)) {
112+
return true;
113+
}
114+
115+
return $line !== $key;
105116
}
106117

107118
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Translation;
4+
5+
use Orchestra\Testbench\TestCase;
6+
7+
class TranslatorTest extends TestCase
8+
{
9+
/**
10+
* Define environment setup.
11+
*
12+
* @param \Illuminate\Foundation\Application $app
13+
* @return void
14+
*/
15+
protected function defineEnvironment($app)
16+
{
17+
$app['translator']->addJsonPath(__DIR__.'/lang');
18+
19+
parent::defineEnvironment($app);
20+
}
21+
22+
public function testItCanGetFromLocaleForJson()
23+
{
24+
$this->assertSame('30 Days', $this->app['translator']->get('30 Days'));
25+
26+
$this->app->setLocale('fr');
27+
28+
$this->assertSame('30 jours', $this->app['translator']->get('30 Days'));
29+
}
30+
31+
public function testItCanCheckLanguageExistsHasFromLocaleForJson()
32+
{
33+
$this->assertTrue($this->app['translator']->has('1 Day'));
34+
$this->assertTrue($this->app['translator']->hasForLocale('1 Day'));
35+
$this->assertTrue($this->app['translator']->hasForLocale('30 Days'));
36+
37+
$this->app->setLocale('fr');
38+
39+
$this->assertFalse($this->app['translator']->has('1 Day'));
40+
$this->assertFalse($this->app['translator']->hasForLocale('1 Day'));
41+
$this->assertTrue($this->app['translator']->hasForLocale('30 Days'));
42+
}
43+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"1 Day": "1 Day",
3+
"30 Days": "30 Days",
4+
"365 Days": "365 Days",
5+
"60 Days": "60 Days",
6+
"90 Days": "90 Days"
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"30 Days": "30 jours",
3+
"365 Days": "365 jours",
4+
"60 Days": "60 jours",
5+
"90 Days": "90 jours"
6+
}

0 commit comments

Comments
 (0)