Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Handle missing translation strings using callback #49040

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
*/
protected $stringableHandlers = [];

/**
* The callback that is responsible for handling missing translation keys.
*
* @var callable|null
*/
protected $missingTranslationKeyCallback;

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

/**
* Create a new translator instance.
*
Expand Down Expand Up @@ -153,6 +167,10 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)
return $line;
}
}

$key = $this->handleMissingTranslationKey(
$key, $replace, $locale, $fallback
);
}

// If the line doesn't exist, we will return back the key which was requested as
Expand Down Expand Up @@ -308,6 +326,48 @@ protected function isLoaded($namespace, $group, $locale)
return isset($this->loaded[$namespace][$group][$locale]);
}

/**
* Handle a missing translation key.
*
* @param string $key
* @param array $replace
* @param string|null $locale
* @param bool $fallback
* @return string
*/
protected function handleMissingTranslationKey($key, $replace, $locale, $fallback)
{
if (! $this->handleMissingTranslationKeys ||
! isset($this->missingTranslationKeyCallback)) {
return $key;
}

// Prevent infinite loops...
$this->handleMissingTranslationKeys = false;

$key = call_user_func(
$this->missingTranslationKeyCallback,
$key, $replace, $locale, $fallback
);

$this->handleMissingTranslationKeys = true;

return $key;
}

/**
* Register a callback that is responsible for handling missing translation keys.
*
* @param callable|null $callback
* @return static
*/
public function handleMissingKeysUsing(?callable $callback)
{
$this->missingTranslationKeyCallback = $callback;

return $this;
}

/**
* Add a new namespace to the loader.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Integration/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,19 @@ public function testItCanCheckLanguageExistsHasFromLocaleForJson()
$this->assertFalse($this->app['translator']->hasForLocale('1 Day'));
$this->assertTrue($this->app['translator']->hasForLocale('30 Days'));
}

public function testItCanHandleMissingKeysUsingCallback()
{
$this->app['translator']->handleMissingKeysUsing(function ($key) {
$_SERVER['__missing_translation_key'] = $key;
return 'callback key';
});

$key = $this->app['translator']->get('some missing key');

$this->assertSame('callback key', $key);
$this->assertSame('some missing key', $_SERVER['__missing_translation_key']);

$this->app['translator']->handleMissingKeysUsing(null);
}
}