php - Change default behavior of the translation function in Laravel
Get the solution ↓↓↓For debugging purpose, I would like that whenever I call__('non-existing-key')
, to see printed for exampleSTRING NOT FOUND
, so that I can crawl my website and search for that specific string...
Are there ways to do it?
Answer
Solution:
Create two files in app/Extended (the folder must be created if doesn't exist):
ExtendedTranslationServiceProvider.php
<?php
namespace App\Extended;
use Illuminate\Translation\TranslationServiceProvider;
class ExtendedTranslationServiceProvider extends TranslationServiceProvider
{
public function register()
{
$this->registerLoader();
$this->app->singleton('translator', function ($app) {
$loader = $app['translation.loader'];
$locale = $app['config']['app.locale'];
$trans = new ExtendedTranslator($loader, $locale);
$trans->setFallback($app['config']['app.fallback_locale']);
return $trans;
});
}
}
ExtendedTranslator.php
<?php
namespace App\Extended;
use Illuminate\Translation\Translator;
class ExtendedTranslator extends Translator
{
public function get($key, array $replace = [], $locale = null, $fallback = true)
{
$trans = parent::get($key, $replace, $locale, $fallback);
if(parent::get($key, [], $locale, $fallback) === $key){
return 'STRING NOT FOUND';
}
return $trans;
}
}
And add a new item in theproviders
array, on app.php:
'providers' => [
...
App\Extended\ExtendedTranslationServiceProvider::class,
]
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xml parsing error: no root element found
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.