Skip to content

Commit f8f0668

Browse files
authored
Do not trigger missing translation key handling when checking existence of that key (#52895)
1 parent a84070d commit f8f0668

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Illuminate/Translation/Translator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,14 @@ public function has($key, $locale = null, $fallback = true)
117117
{
118118
$locale = $locale ?: $this->locale;
119119

120+
$handleMissingTranslationKeys = $this->handleMissingTranslationKeys;
121+
122+
$this->handleMissingTranslationKeys = false;
123+
120124
$line = $this->get($key, [], $locale, $fallback);
121125

126+
$this->handleMissingTranslationKeys = $handleMissingTranslationKeys;
127+
122128
// For JSON translations, the loaded files will contain the correct line.
123129
// Otherwise, we must assume we are handling typical translation file
124130
// and check if the returned line is not the same as the given key.

tests/Integration/Translation/TranslatorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ public function testItCanCheckLanguageExistsHasFromLocaleForJson()
4141
$this->assertTrue($this->app['translator']->hasForLocale('30 Days'));
4242
}
4343

44+
public function testItCanCheckKeyExistsWithoutTriggeringHandleMissingKeys()
45+
{
46+
$this->app['translator']->handleMissingKeysUsing(function ($key) {
47+
$_SERVER['__missing_translation_key'] = $key;
48+
});
49+
50+
$this->assertFalse($this->app['translator']->has('Foo Bar'));
51+
$this->assertFalse(isset($_SERVER['__missing_translation_key']));
52+
53+
$this->assertFalse($this->app['translator']->hasForLocale('Foo Bar', 'nl'));
54+
$this->assertFalse(isset($_SERVER['__missing_translation_key']));
55+
}
56+
4457
public function testItCanHandleMissingKeysUsingCallback()
4558
{
4659
$this->app['translator']->handleMissingKeysUsing(function ($key) {

0 commit comments

Comments
 (0)