Yii2 Advanced Template translations

Hello
I try to translate my module as explained here

In my Module:

<?php

namespace common\modules\profile;
use Yii;

/**

  • profile module definition class
    /
    class Module extends \yii\base\Module
    {
    /
    *

    • {@inheritdoc}
      */
      public $controllerNamespace = ‘common\modules\profile\controllers’;

    /**

    • {@inheritdoc}
      */
      public function init()
      {
      parent::init();
      $this->registerTranslations();

      // custom initialization code goes here
      }

    public function registerTranslations()
    {
    Yii::$app->i18n->translations[‘modules/profile/*’] = [
    ‘class’ => ‘yii\i18n\PhpMessageSource’,
    ‘sourceLanguage’ => ‘en-US’,
    ‘basePath’ => ‘@common/modules/profile/messages’,
    ‘fileMap’ => [],
    ];
    }

    public static function t($category, $message, $params = [], $language = null)
    {
    return Yii::t(’’ . $category, $message, $params, $language);
    }
    }

In common/config/main.php

'i18n' => [
        'translations' => [
            '*' => [
                'class' => 'yii\i18n\PhpMessageSource',
            ],
        ],
    ],

Then in my model:

'lastname' => Module::t('profile', 'Lastname'),  

But the message says it doesn’t find the file:

	The message file for category 'profile' does not exist: /var/www/html/psy-online/frontend/messages/fr-CH/profile.php Fallback file does not exist as well: /var/www/html/psy-online/frontend/messages/fr/profile.php

My Module is under common/modules/profile so why it search the file under frontend/module/profile ?

Any help would be appreciated.
Thanks in advance

Seb

Can you try adding fileMap as described in the guide?

1 Like

Thanks for you reply.
I found a workaround by setting my translation files in @common/messages and use category to distinct module translation files.

I used to use Yii1 but Yii2 brings a lot of improvement, thanks for the good work!

Yeah. Yii 3 is going to be different in a good sense as well.

1 Like