phemellc / yii2-settings

Yii2 Settings Module
Other
151 stars 74 forks source link

Unable to locate message source for category 'extensions/yii2-settings/settings'. #51

Closed zhukovsergei closed 8 years ago

zhukovsergei commented 8 years ago

The same issue had already created here but author hadn't written the solution. AS i understood, the module can't find out locate file, or not?

demogorgorn commented 8 years ago

The same situation

arisk commented 8 years ago

I'm using the standard Yii2 translation settings for modules. http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html#module-translation If your language doesn't yet exist you can configure the i18n component or optionally submit a PR for your translation.

zhukovsergei commented 8 years ago

I have added in config the next:

'i18n' => [
      'translations' => [
        '*' => [
          'class' => 'yii\i18n\PhpMessageSource',
          'basePath' => '@app/messages', // if advanced application, set @frontend/messages
          'sourceLanguage' => 'ru-RU',
          'fileMap' => [
              //'main' => 'main.php',
          ],
        ],
      ],
    ],
zacksleo commented 7 years ago

@webadequate The reason I found is that the "yii2-settings module is not init when this issue occurs" .

When I submit the form, it will call the function in the "yii2-settings/models/Setting.php", while the yii2-module is not init (which will register i18n).

Here is the code what I do ine the controller.

Controller:

public function beforeAction($action)
  {
      Yii::$app->getModule('settings')->init(); // make sure this module is init first
      return parent::beforeAction($action);
  }

function actions(){
   return [
        //....
            'site-settings' => [
                'class' => 'pheme\settings\SettingsAction',
                'modelClass' => 'app\models\Site',
                //'scenario' => 'site', // Change if you want to re-use the model for multiple setting form.
                'viewName' => 'site-settings'   // The form we need to render
            ],
        //....
    ];
}
arisk commented 7 years ago

You may look into extending BaseSetting for your model. It doesn't use any module translations.

TrueXakeP commented 7 years ago

I don't see BaseSetting there. What do you mean?

I don't see a reason to init module which I'm not going to use right now. So I came with this solution:

class RegisterSettingTranslationHelper extends \pheme\settings\Module {
    public static function fixRegisterTranslations() {
        $m = new parent([]);
        $m->registerTranslations();
        unset($m);
    }
}

class SiteController extends Controller {
    public function beforeAction($action) {
        if ($action instanceof \pheme\settings\SettingsAction) {
            RegisterSettingTranslationHelper::fixRegisterTranslations();
        }
        return parent::beforeAction($action);
    }
}

The registerTranslations() does not use $this actually so I think it can be made static and even public.

Actually I use (new GalleryModule([]))->registerTranslations(); with another module with public access of it and it looks simpler.