Skip to content

Commit

Permalink
Do not trigger missing translation key handling when checking existen…
Browse files Browse the repository at this point in the history
…ce of that key (#52895)
  • Loading branch information
gdebrauwer authored Sep 24, 2024
1 parent a84070d commit f8f0668
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f8f0668

Please sign in to comment.