how to use multilanguage in yii2? [duplicate]

2019-08-12 04:54发布

This question already has an answer here:

How to create multilanguage app in yii2?
Is there any preinstall message in yii2?

config:

'language' => 'es',
'components' => [
    'i18n' => [
        'translations' => [
            'app*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                //'basePath' => '@app/messages',
                'sourceLanguage' => 'ru-RU',

                'fileMap' => [
                    'app' => 'app.php',
                    'app/error' => 'error.php',
                ],
            ],
        ],
    ],

view :

echo \Yii::t('app', 'I am a message!');
$username = 'Alexander';
echo \Yii::t('app', 'Hello, {username}!', [
    'username' => $username,
]);

1条回答
爷、活的狠高调
2楼-- · 2019-08-12 05:08

Try this:

Edit config.php in frontend or backend

'language' => 'ru',
'i18n' => [
        'translations' => [
            'app*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@app/messages',
                'fileMap' => [
                    'app' => 'app.php',
                ],
            ],
        ],
    ],

create app.php in frontend/messages/ru/app.php or backend/messages/ru/app.php

View:

echo \Yii::t('app', 'I am a message!');
查看更多
登录 后发表回答