Last active
September 21, 2018 08:33
-
-
Save paulund/e02eccd96214477d14d8d55bb347e8e3 to your computer and use it in GitHub Desktop.
Laravel log missing translation files https://paulund.co.uk/laravel-log-missing-translation-strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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