Extend translations to generate log entries for missing translations.

This commit is contained in:
2024-11-27 08:53:24 +01:00
parent 88b8c3f7b0
commit 84951052f0
2 changed files with 38 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Translation\Translator;
class AppServiceProvider extends ServiceProvider
{
@@ -14,6 +15,12 @@ class AppServiceProvider extends ServiceProvider
if ($this->app->environment() === 'local') {
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
$this->app->register(TelescopeServiceProvider::class);
$this->app->extend('translator', function (Translator $translator) {
$trans = new \App\Translator($translator->getLoader(), $translator->getLocale());
$trans->setFallback($translator->getFallback());
return $trans;
});
}
}

31
app/Translator.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
namespace App;
use Illuminate\Support\Facades\Log;
use Illuminate\Translation\Translator as BaseTranslator;
class Translator extends BaseTranslator
{
/**
* @param string $key
* @param array $replace
* @param null $locale
* @param bool $fallback
*
* @return array|null|string|void
*/
public function get($key, array $replace = [], $locale = null, $fallback = true)
{
$translation = parent::get($key, $replace, $locale, $fallback);
if ($translation === $key) {
Log::warning('Language item could not be found.', [
'language' => $locale ?? config('app.locale'),
'id' => $key,
'url' => config('app.url')
]);
}
return $translation;
}
}