Peon
Peon

Reputation: 8030

How to automatically insert new translation into database in Yii 2

I just started using Yii 2 and already running into some issues I can't figure out myself.

I set up the translator, to use my DB instead of files like this ( and two tables message and source_message):

'i18n' => [
    'translations' => [
        'app*' => [
            'class' => 'yii\i18n\DbMessageSource',
            //'basePath' => '@app/messages',
            //'sourceLanguage' => 'en',
        ],
    ],
],

I even set the translator parameter to true:

public $forceTranslation = true;

Now I create the text to translate like this:

<?=Yii::t('app', 'Congratulations!');?>

When I check the debug info, I see, that there has been only one query:

SELECT
    "t1"."message" AS "message",
    "t2"."translation" AS "translation"
FROM
    "source_message" "t1",
    "message" "t2"
WHERE
    t1.id = t2.id AND
    t1.category = 'app' AND
    t2.language = 'en'

This doesn’t return anything, since there are no translations present. Now, what I can't figure out, how to make the system auto-create a new translation, for the missing translations.

Is such a feature already built in or do I need to create one my self? Because I couldn't find anything on the internet.

And, if there is not, what is the right way to do so?

Upvotes: 1

Views: 2776

Answers (1)

Peon
Peon

Reputation: 8030

I found this script by Aleksandr Zelenin that did exactly, what I needed: https://github.com/zelenin/yii2-i18n-module

Component uses yii\i18n\MissingTranslationEvent for auto-add of missing translations to database

Upvotes: 1

Related Questions