Internationalization in Yii2 (Database)

I’m kinda struggling what is going on right now.

I want to set up a site with multilanguage-functionality. For that, I read the topic about Internationalization in the guides. I don’t want to use the text-/file-version, I want to use the DB instead.

I found out, that there is something like the DbMessageSource. I applied the migration and have the following tables in my DB:



In my config-file, I addes these lines:

'components' => [

        'cache' => [

            'class' => 'yii\caching\FileCache',


        'i18n' => [

            'translations' => [

                '*' => [

                    'class' => 'yii\i18n\DbMessageSource',

                    'forceTranslation' => true,

                    'sourceMessageTable' => '{{%source_message}}',

                    'messageTable' => '{{%message}}',

                    'enableCaching' => false,

                    'cachingDuration' => 3600,

                    'sourceLanguage' => 'en_US'





In my view, I call the translation-function like this:

[b]echo yii::t('header_menu', 'news'); [/b]

It is actually displaying "news", but regardless of the language I set, nothing is stored in the DB. Am I missing something? I thought every string, which is in a translate-function would be stored in the DB?

Thanks in advance

Not quite, every string passed to Yii::t will be checked for a translation existing in the database.

You need to fill the database so that Yii can read your translations from it.

So, if a String doesn’t exist in the DB, it won’t be translated, right? Is there a way to extract every string in the t()-function to enrich the DB? I think I saw something similar for getTextTranslation or something?


Not to my knowledge, no.

Yes there is: