如何在yii2中使用多语言?

时间:2015-10-01 08:04:13

标签: php yii2 multilingual

如何在yii2中创建多语言应用程序?
是否在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',
                ],
            ],
        ],
    ],

观点:

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

1 个答案:

答案 0 :(得分:0)

试试这个:

在前端或后端编辑config.php

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

app.phpfrontend/messages/ru/app.php

中创建backend/messages/ru/app.php

查看:

echo \Yii::t('app', 'I am a message!');