Reputation: 100
I'm using the Yii2 advanced template. I have implemented the translations(i18n) following this tutorial and reviewing this SO question. YES, I read the documentation.
My translations are not working and I found out in the debugger that it's looking for he translations in the frontend folder instead of he common folder where the message/extract created the translations files:
The message file for category 'app' does not exist: localhost/frontend/messages/es/app.php
I know the easiest thing would be to move the messages folder to the frontend folder since I'm not using translations in the backend, but I'd like to understand what I'm doing wrong.
This is my i18n file located in common/config:
'sourcePath' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR,
'languages' => ['es'], //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,
This is my common/config/main file
'i18n' => [
'translations' => [
'frontend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
'backend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
],
],
This is where the aliases are defined (default. common/config/bootstrap) and echoing @common returns common:
Yii::setAlias('@common', dirname(__DIR__));
Yii::setAlias('@frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console');
Upvotes: 3
Views: 1790
Reputation: 4390
Using your configuration, the translations must be called using:
Yii::t('frontend','Frontend_string');
for the frontend and,
Yii::t('backend','Backend_string');
for the backend content.
See the point 6. in the tutorial
Upvotes: 1
Reputation: 3008
Your code is correct, but from the error message seems that you are calling:
Yii::t('app', '...');
Instead in your common/config/main you have declared entries for 'frontend*' and 'backend*', but not for 'app*'. So Yii will continue search inside frontend repository folder.
The common/config/main should contain (if you want to use Yii::t('app',...')
):
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
Upvotes: 2