From f8f06684855a52fc3a3ab302030cdcb08d965a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Debrauwer?= Date: Tue, 24 Sep 2024 15:45:31 +0200 Subject: [PATCH] Do not trigger missing translation key handling when checking existence of that key (#52895) --- src/Illuminate/Translation/Translator.php | 6 ++++++ tests/Integration/Translation/TranslatorTest.php | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index ccda77d14717..b7001838d3ce 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -117,8 +117,14 @@ public function has($key, $locale = null, $fallback = true) { $locale = $locale ?: $this->locale; + $handleMissingTranslationKeys = $this->handleMissingTranslationKeys; + + $this->handleMissingTranslationKeys = false; + $line = $this->get($key, [], $locale, $fallback); + $this->handleMissingTranslationKeys = $handleMissingTranslationKeys; + // 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. diff --git a/tests/Integration/Translation/TranslatorTest.php b/tests/Integration/Translation/TranslatorTest.php index de82aabce403..a75950335f72 100644 --- a/tests/Integration/Translation/TranslatorTest.php +++ b/tests/Integration/Translation/TranslatorTest.php @@ -41,6 +41,19 @@ public function testItCanCheckLanguageExistsHasFromLocaleForJson() $this->assertTrue($this->app['translator']->hasForLocale('30 Days')); } + public function testItCanCheckKeyExistsWithoutTriggeringHandleMissingKeys() + { + $this->app['translator']->handleMissingKeysUsing(function ($key) { + $_SERVER['__missing_translation_key'] = $key; + }); + + $this->assertFalse($this->app['translator']->has('Foo Bar')); + $this->assertFalse(isset($_SERVER['__missing_translation_key'])); + + $this->assertFalse($this->app['translator']->hasForLocale('Foo Bar', 'nl')); + $this->assertFalse(isset($_SERVER['__missing_translation_key'])); + } + public function testItCanHandleMissingKeysUsingCallback() { $this->app['translator']->handleMissingKeysUsing(function ($key) {