Hi all!
I am making my first proyect with Yii2, all is fine for the moment . But I have my first problem when I try implement the transaltion system. I will post my steps. I want use this translations to Spanish and English (Default)
common/config/main.php
'language' => 'es',
'sourceLanguage'=> 'en',
....
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
// 'sourceLanguage' => 'en-US',
/*'fileMap' => [
'app' => 'app.php',
'app/error' => 'error.php',
],*/
],
],
],
common/config/i18n.php
return [
'sourcePath' => __DIR__. '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR,
'languages' => ['es', 'en'], //Add languages to the array for the language files to be generated.
'translator' => 'Yii::t',
'sort' => false,
'removeUnused' => false,
'only' => ['*.php'],
'except' => [
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'/messages',
'/vendor',
],
'format' => 'php',
'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'messages',
'overwrite' => true,
];
I have /common/messages/en/app.php and /common/messages/en/app.php with same array of values.
return [
'Generic_Guest'=>'Guest',
'Sidebar_Hello' => 'Hello',
'Signup' => 'Signup',
];
Finally, I am trying translate this in one of my views.
<?
$username = (Yii::$app->user->isGuest)? Yii::t('app', 'Generic_Guest') : Yii::$app->user->identity->username;
echo Yii::t('app', 'Sidebar_Hello, {username}!', [
'username' => $username,
]);
?>
But I found this result:
Sidebar_Hello, Generic_Guest!