A
A
Alex1222016-10-27 16:25:54
Yii
Alex122, 2016-10-27 16:25:54

How to override base method in yii2?

Hello! The question is as follows. There is a BaseYii base class in Yii2. This class contains the t() method. Right now I'm using yii\i18n\DbMessageSource for translations that are stored in the database. The translation can be obtained by calling the t() method with the appropriate arguments.
The task is to add translations to the database, if they are not already there, when calling the t () method. For example, we call t('category', 'title'), it itself should check if there is a translation in the database and add it if it is not there. How can I do this if I want to call the same Yii::t() in my application?
Forgive me if I didn't describe it exactly, this is my first question.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Khomenko, 2016-10-27
@Alex122

Everything is ready, no need to redefine anything.
You just need to configure the component correctly.
www.yiiframework.com/doc-2.0/guide-tutorial-i18n.h...

'components' => [
    // ...
    'i18n' => [
        'translations' => [
            'app*' => [
                // ...
                'on missingTranslation' => ['app\components\TranslationEventHandler', 'handleMissingTranslation']
            ],
        ],
    ],
],

Be aware that stuff like this should only work with YII_ENV === 'dev'

M
Maxim Timofeev, 2016-10-27
@webinar

You can override the method by creating your own class, including it in the config and inheriting it from yii\i18n\DbMessageSource
BUT!!! Why is all this necessary? Isn't it easier to make a button in the admin panel or run a script by cron that will parse the view folder and collect those that are not crammed into the database? The action will be resource-intensive, but 1 time in a certain period of time. Made changes - launched, order. And in your idea, this will be an additional action for each rendering of the page, and for each call to t ().

S
Sergey Doniy, 2016-10-27
@doniys_a

https://github.com/lajax/yii2-translate-manager
It has a scan action out of the box that collects and adds new texts from Yii::t to the database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question