Skip to content

Commit

Permalink
Add functionality to avoid infinite loops when the handle missing tra…
Browse files Browse the repository at this point in the history
…nslation callback itself tries to translation a missing translation.
  • Loading branch information
Dean Wunder committed Nov 19, 2023
1 parent b70ed2e commit 9f8eb09
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
*/
protected $stringableHandlers = [];

/**
* Whether missing translation keys should be handled.
*
* @var bool
*/
protected $handleMissingTranslationKeys = true;

/**
* The callback that is responsible for handling missing translation keys.
*
Expand Down Expand Up @@ -183,12 +190,24 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)
*/
protected function handleMissingTranslationKey($key, $replace, $locale, $fallback)
{
// Call user defined missing translation handler callback if set.
if (isset(static::$missingTranslationKeyCallback)) {
return call_user_func(static::$missingTranslationKeyCallback,
$key, $replace, $locale, $fallback
);
// Avoid infinite loops: don't handle any missing translations which are in
// the user defined callback.
if (! $this->handleMissingTranslationKeys) {
return $key;
}

// Return the key unchanged if there is no user defined callback set.
if (! isset(static::$missingTranslationKeyCallback)) {
return $key;
}

// Call user defined missing translation handler callback, prevent infinite loops.
$this->handleMissingTranslationKeys = false;
$key = call_user_func(static::$missingTranslationKeyCallback,
$key, $replace, $locale, $fallback
);
$this->handleMissingTranslationKeys = true;

return $key;
}

Expand Down

0 comments on commit 9f8eb09

Please sign in to comment.