Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Translation\Translator;
use App\Translator\JsonTranslator;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// Override the JSON Translator
$this->app->extend('translator', function (Translator $translator) {
$trans = new JsonTranslator($translator->getLoader(), $translator->getLocale());
$trans->setFallback($translator->getFallback());
return $trans;
});
}
}
<?php
namespace App\Translator;
use Illuminate\Support\Facades\Log;
use Illuminate\Translation\Translator as BaseTranslator;
/**
* Class JsonTranslator
* @package App\Translator
*/
class JsonTranslator 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment