Yii2 translating data stored in database

I’m working with Yii2 Framework, and already configured i18n component using this guide:

So, I can now translate strings within my php files using Yii::t() function. Translatable strings are extracted using $ ./yii message/extract console command, which generates proper translation files.

I now need to display translations for strings stored in the database.

I could use Yii:t() with a variable instead of a string as an argument like this

echo Yii:t('app', $some_string_from_db );

and make a new php file with some code like this


function dbStringsToTranslate() {
    $o = Yii::t('app','db english string 1'); 
    $o.= Yii::t('app','db english string 2'); 
    $o.= Yii::t('app','db english string 3'); 
    return $o;

This way $ ./yii message/extract command will find the needed translations.

This is working Ok, but of course $ ./yii message/extract is throwing some warnings anywhere I use Yii:t() with variables.

Skipping line 39. Make sure both category and message are static strings.

I think this is not a big deal, but well, here is my question:

Is this a right way to translate strings stored in a database?

Is there a better way to accomplish this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s