更改 Laravel 中翻译功能的默认行为

Change default behavior of the translation function in Laravel

出于调试目的,我希望每当调用 __('non-existing-key') 时都能看到打印出来的内容,例如 STRING NOT FOUND,这样我就可以抓取我的网站并搜索该特定字符串...

有什么办法吗?

app/Extended中创建两个文件(文件夹不存在必须创建):

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;
    }
}

并在 providers 数组中添加一个新项目,在 app.php:

'providers' => [
    ...
    App\Extended\ExtendedTranslationServiceProvider::class,
]