yii2yii-modules

Include modules translation file outside module in Yii


I have Yii application with following structure
application/

    /framework
    /protect
        /modules
           /report
              /translation
               lang.php
        /views
        /controller
        /model

I have working language translation inside module, But When I try to access language file lang.php from the view of the protected directory outside modules with following code

Yii::t('ReportModule.lang','Report Title');

It shows error.

include(VreportModule.php): failed to open stream: No such file or directory

How to make modules language file accessible outside the module ??


Solution

  • In the Advanced template I did this in main.php, You probably need to do this inside your web.php

     'i18n' => [
            'translations' => [
                'general*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@backend/messages/general',
                    'sourceLanguage' => 'en',
                ],
              ],
           ],
    

    And now I can access Yii::t('general','message'); Everywhere The folder structure is:

    \BasePath
        \language_code example( nl or nl_NL )
           \language file this time it's general.php
    

    More information: Yii2 documentation