phemellc / yii2-settings

Yii2 Settings Module
Other
151 stars 74 forks source link

Register translations in Settings. Add polish translation. #43

Closed ksideks closed 8 years ago

ksideks commented 8 years ago

Invalid Configuration – yii\base\InvalidConfigException

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

throw new InvalidConfigException("Unable to locate message source for category '$category'.");

arisk commented 8 years ago

Hi. Since the component doesn't actually use any translations I don't think it's a good idea to register the translations there. The translations are registered already in the module file.

ksideks commented 8 years ago

I use only component in config file.

'components' => [ ... 'settings' => [ 'class' => 'pheme\settings\components\Settings', 'cache' => false, 'frontCache' => false, ], ],

When I try: Yii::$app->settings->set('var.val',1);

I have error:

  1. in /home/.../vendor/yiisoft/yii2/i18n/I18N.php at line 200
                $source = $this->translations['*'];
                if ($source instanceof MessageSource) {
                    return $source;
                } else {
                    return $this->translations[$category] = $this->translations['*'] = Yii::createObject($source);
                }
            }
        }

        throw new InvalidConfigException("Unable to locate message source for category '$category'.");
    }
}

2. in /home/.../vendor/yiisoft/yii2/i18n/I18N.php at line 88 – yii\i18n\I18N::getMessageSource('extensions/yii2-settings/setting...')
3. in /home/.../vendor/yiisoft/yii2/BaseYii.php at line 501 – yii\i18n\I18N::translate('extensions/yii2-settings/setting...', '{attribute} "{value}" already ex...', [], 'pl-PL')
4. in /home/.../vendor/pheme/yii2-settings/Module.php at line 71 – yii\BaseYii::t('extensions/yii2-settings/setting...', '{attribute} "{value}" already ex...', [], null)

     * @param array $params
     * @param null $language
     * @return string
     */
    public static function t($category, $message, $params = [], $language = null)
    {
        return Yii::t('extensions/yii2-settings/' . $category, $message, $params, $language);
    }
}

5. in /home/.../vendor/pheme/yii2-settings/models/Setting.php at line 100 – pheme\settings\Module::t('settings', '{attribute} "{value}" already ex...')

            [['section', 'key'], 'string', 'max' => 255],
            [
                ['key'],
                'unique',
                'targetAttribute' => ['section', 'key'],
                'message' =>
                    Module::t('settings', '{attribute} "{value}" already exists for this section.')
            ],
            ['type', 'in', 'range' => array_keys($this->getTypes(false))],
            [['type', 'created', 'modified'], 'safe'],
            [['active'], 'boolean'],
        ];
    }

6. in /home/.../vendor/yiisoft/yii2/base/Model.php at line 441 – pheme\settings\models\Setting::rules()
7. in /home/.../vendor/yiisoft/yii2/base/Model.php at line 409 – yii\base\Model::createValidators()
8. in /home/.../vendor/yiisoft/yii2/base/Model.php at line 185 – yii\base\Model::getValidators()
9. in /home/.../vendor/yiisoft/yii2/base/Model.php at line 345 – yii\base\Model::scenarios()
10. in /home/.../vendor/yiisoft/yii2/db/ActiveRecord.php at line 527 – yii\base\Model::validate(null)
11. in /home/.../vendor/yiisoft/yii2/db/BaseActiveRecord.php at line 595 – yii\db\ActiveRecord::update(true, null)
12. in /home/.../vendor/pheme/yii2-settings/models/Setting.php at line 213 – yii\db\BaseActiveRecord::save()

        if ($type !== null) {
            $model->type = $type;
        } else {
            $model->type = gettype($value);
        }

        return $model->save();
    }

    /**
     * @inheritdoc
     */
    public function activateSetting($section, $key)

13. in /home/.../vendor/pheme/yii2-settings/components/Settings.php at line 150 – pheme\settings\models\Setting::setSetting('var', 'val', 1, null)

        if (is_null($section)) {
            $pieces = explode('.', $key);
            $section = $pieces[0];
            $key = $pieces[1];
        }

        if ($this->model->setSetting($section, $key, $value, $type)) {
            if ($this->clearCache()) {
                return true;
            }
        }
        return false;
    }

14. in /home/.../backend/controllers/SettingsController.php at line 40 – pheme\settings\components\Settings::set('var.val', 1)

    public function actionIndex()
    {
        Yii::$app->settings->set('var.val',1);
        return $this->render('index');
    }
ksideks commented 8 years ago

With module in config:

'modules' => [ 'settings' => [ 'class' => 'pheme\settings\Module', 'sourceLanguage' => 'en' ], ... ],

I have the same error.

arisk commented 8 years ago

Ok. I got it. This bug was introduced recently by adding translated error messages to the model. It makes it difficult to use the component without being inside the module. The problem is setting the source language which is done in the module. I think a better solution might be to remove the translated error messages from beforeSave. What do you think?

arisk commented 8 years ago

I cherry picked the polish translation from your PR. 722056a1acfc49a93a0ef7f728cd40931155cc2d The component should work without the need for a module now. Thanks for reporting.

ksideks commented 8 years ago

Thanks. Component works correctly.