<commit_before><?php
/**
 * @link https://github.com/LAV45/yii2-translated-behavior
 * @copyright Copyright (c) 2015 LAV45!
 * @author <NAME> <<EMAIL>>
 * @license http://opensource.org/licenses/BSD-3-Clause
 */

namespace lav45\translate;

use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\validators\Validator;
use yii\validators\RequiredValidator;

/**
 * Class TranslatedBehavior
 * @package lav45\translate\TranslatedBehavior
 *
 * @property ActiveRecord|TranslatedTrait $owner
 */
class TranslatedBehavior extends Behavior
{
    /**
     * @var string the translations relation name
     */
    public $translateRelation;
    /**
     * @var string the translations model language attribute name
     */
    public $languageAttribute = 'lang_id';
    /**
     * @var string[] the list of translateAttributes to be translated
     */
    private $_translate_attributes;
    /**
     * @var string current translate language
     */
    private $_language;

    /**
     * @param string $language
     * @return ActiveRecord
     */
    public function setLanguage($language)
    {
        if (!empty($language)) {
            $this->_language = $language;
        }
    }

    /**
     * @return string
     */
    public function getLanguage()
    {
        if ($this->_language === null) {
            $this->_language = substr(Yii::$app->language, 0, 2);
        }
        return $this->_language;
    }

    /**
     * @return string
     */
    protected function getSourceLanguage()
    {
        return substr(Yii::$app->sourceLanguage, 0, 2);
    }

    /**
     * @inheritdoc
     */
    public function events()
    {
        return [
            ActiveRecord::EVENT_INIT => 'initEvent',
            ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete',
            ActiveRecord::EVENT_AFTER_VALIDATE => 'afterValidate',
            ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
            ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
        ];
    }

    public function initEvent()
    {
        $keys = array_keys($this->getTranslateAttributes());
        $this->owner->validators[] = Validator::createValidator('safe', $this->owner, $keys);
    }

    /**
     * @return ActiveRecord[]
     */
    protected function getTranslateRelations()<commit_msg>remove initEvent<commit_after><?php
/**
 * @link https://github.com/LAV45/yii2-translated-behavior
 * @copyright Copyright (c) 2015 LAV45!
 * @author <NAME> <<EMAIL>>
 * @license http://opensource.org/licenses/BSD-3-Clause
 */

namespace lav45\translate;

use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\validators\RequiredValidator;

/**
 * Class TranslatedBehavior
 * @package lav45\translate\TranslatedBehavior
 *
 * @property ActiveRecord|TranslatedTrait $owner
 */
class TranslatedBehavior extends Behavior
{
    /**
     * @var string the translations relation name
     */
    public $translateRelation;
    /**
     * @var string the translations model language attribute name
     */
    public $languageAttribute = 'lang_id';
    /**
     * @var string[] the list of translateAttributes to be translated
     */
    private $_translate_attributes;
    /**
     * @var string current translate language
     */
    private $_language;

    /**
     * @param string $language
     * @return ActiveRecord
     */
    public function setLanguage($language)
    {
        if (!empty($language)) {
            $this->_language = $language;
        }
    }

    /**
     * @return string
     */
    public function getLanguage()
    {
        if ($this->_language === null) {
            $this->_language = substr(Yii::$app->language, 0, 2);
        }
        return $this->_language;
    }

    /**
     * @return string
     */
    protected function getSourceLanguage()
    {
        return substr(Yii::$app->sourceLanguage, 0, 2);
    }

    /**
     * @inheritdoc
     */
    public function events()
    {
        return [
            ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete',
            ActiveRecord::EVENT_AFTER_VALIDATE => 'afterValidate',
            ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
            ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
        ];
    }

    /**
     * @return ActiveRecord[]
     */
    protected function getTranslateRelations()
{'fraction_non_alphanumeric': 0.12803481447549245, 'fraction_numerical': 0.008245533669262483, 'mean_word_length': 2.4796812749003982, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 34, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4588011', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1229, 'n_words': 265}