Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

php - Change default behavior of the translation function in Laravel

For debugging purpose, I would like that whenever I call __('non-existing-key'), to see printed for example STRING NOT FOUND, so that I can crawl my website and search for that specific string...

Are there ways to do it?

question from:https://stackoverflow.com/questions/66054474/change-default-behavior-of-the-translation-function-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create two files in app/Extended (the folder must be created if doesn't exist):

ExtendedTranslationServiceProvider.php

<?php

namespace AppExtended;

use IlluminateTranslationTranslationServiceProvider;

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

use IlluminateTranslationTranslator;

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 the providers array, on app.php:

'providers' => [
    ...
    AppExtendedExtendedTranslationServiceProvider::class,
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...