简体   繁体   中英

How to configure gettext with Yii::t() in Yii2 advanced?

I'm installed Yii2 advanced template and configure Yii::t() with PhpMessageSource, but i need configure it with GettextMessageSource. I'm added to common/config/main-local.php this code:

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

Put at frontend/messages/ru-RU app.po file sample text:

msgid "TEST"
msgstr "Текст"

Add in view file:

Yii::t('app', 'TEST');

But it doesn't work, where i need to store .po files or what i'm doing wrong?

'basePath' => '@app/messages',

change to 'basePath' => '@common/messages', or 'basePath' => '@root/common/messages

and replace your messages to common/messages.

I had also problem with GettextMessageSource. Check your po file if it has line msgctxt It should look like this:

msgctxt "app"
msgid "Settings"
msgstr "Ustawienia"

您还可以传递一个参数Yii :: t()

Yii::t("app", "some text: {someVariable}",["someVariable"=>$whatEverYouWant]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM