【问题标题】:CakePHP Translate Behavior with related modelsCakePHP 使用相关模型翻译行为
【发布时间】:2013-08-05 14:23:47
【问题描述】:

我正在为相关模型(hasOne、hasMany 或 HABTM)使用 Translate Behavior/i18n 寻找更好的解决方案。 CakePHP 1.x 和 2.x 不支持这个。

我的解决方案非常难看,但是可以:

    if(Configure::read('Config.language') !== DEFAULT_LANGUAGE) {
        $this->{$this->modelClass}->locale = array(Configure::read('Config.language'), DEFAULT_LANGUAGE);

        if(is_array($this->{$this->modelClass}->belongsTo)) {
            foreach($this->{$this->modelClass}->belongsTo as $relation => $model) {
                $this->{$this->modelClass}->$model['className']->locale = array(Configure::read('Config.language'), DEFAULT_LANGUAGE);
            }
        } elseif(is_array($this->{$this->modelClass}->hasOne)) {
            foreach($this->{$this->modelClass}->hasOne as $relation => $model) {
                $this->{$this->modelClass}->$model['className']->locale = array(Configure::read('Config.language'), DEFAULT_LANGUAGE);
            }
        } elseif(is_array($this->{$this->modelClass}->hasMany)) {
            foreach($this->{$this->modelClass}->hasMany as $relation => $model) {
                $this->{$this->modelClass}->$model['className']->locale = array(Configure::read('Config.language'), DEFAULT_LANGUAGE);
            }
        }
    } else {
        $this->{$this->modelClass}->locale = DEFAULT_LANGUAGE;
    }

也许你有更好的解决方案,你可以告诉我:)

【问题讨论】:

    标签: cakephp internationalization


    【解决方案1】:

    这是我想出的解决方案。

    // app/Model/AppModel.php
    public function afterFind($results, $primary = false) {
        // if getting associated data
        if($primary === false) {
            // check for translate behavior
            foreach(array_keys($this->actsAs) as $behavior) {
                if(preg_match('/^(T|t)ranslate$/', $behavior)) {
                    // set locale to lookup translation
                    $currentLanguage = Configure::read('Config.language');
                    if($currentLanguage !== DEFAULT_LANGUAGE) {
                        $this->locale = array($currentLanguage, DEFAULT_LANGUAGE);  
                    } else {
                        $this->locale = $currentLanguage;   
                    }
                    // if multi-dimensional array
                    if(count($results) != count($results, COUNT_RECURSIVE)) {
                        foreach($results as &$model) {
                            if(is_array($model)){
                                // if multiple models
                                if(isset($model[$this->name][0])) {
                                    foreach($model[$this->name] as &$associatedModel) {
                                        // get model with translations
                                        $res = $this->find('first', array(
                                            'conditions' => array("{$this->alias}.{$this->primaryKey} = ".$associatedModel[$this->primaryKey]),
                                            'contain' => false
                                        ));
                                        // add translated fields to results
                                        $diff = array_diff_assoc($res[$this->name], $associatedModel);
                                        if(count($diff)) {
                                            $associatedModel = array_merge($associatedModel, $diff);
                                        }
                                    }
                                } else if(!empty($model[$this->name])) {
                                    // get model with translations
                                    $res = $this->find('first', array(
                                        'conditions' => array("{$this->alias}.{$this->primaryKey} = ".$model[$this->name][$this->primaryKey]),
                                        'contain' => false
                                    ));
                                    // add translated fields to results
                                    $diff = array_diff_assoc($res[$this->name], $model[$this->name]);
                                    if(count($diff)) {
                                        $model[$this->name] = array_merge($model[$this->name], $diff);
                                    }
                                }
                            }
                        }
                    } else {
                        // get model with translations
                        $res = $this->find('first', array(
                            'conditions' => array("{$this->alias}.{$this->primaryKey} = ".$results[$this->primaryKey]),
                            'contain' => false
                        ));
                        // add translated fields to results
                        $diff = array_diff_assoc($res[$this->name], $results);
                        if(count($diff)) {
                            $results = array_merge($results, $diff);
                        }
                    }
                }
            }           
        }
        return $results;
    }
    

    【讨论】:

    • 看起来好多了,我会测试。
    【解决方案2】:

    我在插件 appmodel 中使用了这个 afterFind 方法,但没有区别

    这是我的控制器中的 admin_index 函数:

        $this->Certificate->locale = $this->Session->read('Config.language');
        $this->Certificate->CertificateType->locale =  $this->Session->read('Config.language');
        $this->Certificate->recursive = 0;
        $this->set('certificates', $this->paginate());
    

    但是所属的模型(CertificateType)没有翻译!这几天让我发疯了。

    【讨论】:

      猜你喜欢
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      相关资源
      最近更新 更多