Skip to content

Commit

Permalink
Translation: Log a warning for any unknown lang keys
Browse files Browse the repository at this point in the history
Based on https://laravel.com/docs/10.x/localization#handling-missing-translation-strings.

Use "resolving" callback instead of using the Log facade / the 'log'
service because if we do that it will force the deferred
LogServiceProvider to be registered on every request, essentially
making it not deferred any more.

Similar to how Model::shouldBeStrict() is only called when not
in production, we only throw an exception when not in production.
  • Loading branch information
Phil Bates committed Jan 19, 2024
1 parent 1d678f6 commit 431862b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace App\Providers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Illuminate\Translation\Translator;
use OutOfBoundsException;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -14,7 +17,19 @@ class AppServiceProvider extends ServiceProvider
*/
public function register(): void
{
//
$this->app->resolving('translator', static function (Translator $translator): void {
$translator->handleMissingKeysUsing(static function (string $key): string {
$message = "Missing translation key [{$key}] detected.";

if (!$this->isProduction()) {

Check failure on line 24 in app/Providers/AppServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Undefined variable: $this
throw new OutOfBoundsException($message);
}

Log::warning($message);

return $key;
});
});
}

/**
Expand Down

0 comments on commit 431862b

Please sign in to comment.