Laravel comes with a built-in localization feature. But sometimes your translation files doesn't have a requested translation, and even if fallback locale is used you generally doesn't notice those missing translations and they can be hard to detect.
Using this package and its fallback functionality, you can decide what should happen. The idea of this package is to setting up a callback function with your custom code that is called everytime a translation key is not found.
This package could be used in development, but also in a production environment.
You can install the package via composer:
composer require smknstd/laravel-missing-translationTo detect missing translations, you have to swap the Laravel TranslationServiceProvider with a custom provider.
In your config/app.php, comment out the original TranslationServiceProvider and add the one from this package:
'providers' => [
...
//'Illuminate\Translation\TranslationServiceProvider',
'Smknstd\LaravelMissingTranslation\TranslationServiceProvider',
]Then to set up the fallback system you need to call static method on the facade Smknstd\LaravelMissingTranslation\Facades\MissingTranslation.
Typically, you would put this in a service provider of your own.
You have to register some code you want to run, by passing a closure. It will be used as a callback function and will be executed everytime a translation key is not found. It lets you execute some custom code like logging something or contact a remote service for example:
// typically, in a service provider
use Smknstd\LaravelMissingTranslation\Facades\MissingTranslation;
MissingTranslation::fallback(function (
string $translationKey,
string $locale,
?string $fallbackLocale = null,
?string $fallbackTranslation = null,
) {
// do something (ex: logging, alerting, etc)
Log::warning('Some translation key is missing', [
'key' => $translationKey,
'locale' => $locale,
'fallback_locale' => $fallbackLocale,
'fallback_translation' => $fallbackTranslation,
]);
});If the closure returns a string, it will be used as the fallback translation:
// typically, in a service provider
use Smknstd\LaravelMissingTranslation\Facades\MissingTranslation;
use App\Service\MyRemoteTranslationService;
MissingTranslation::fallback(function (
string $translationKey,
string $locale,
string $fallbackLocale,
string $fallbackTranslation,
) {
return MyRemoteTranslationService::getAutomaticTranslation(
$fallbackTranslation,
$fallbackLocale,
$locale
);
});composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
