2013-08-05 82 views
0

我正在尋找更好的解決方案使用Translate Behavior/i18n的相關模型(hasOne,hasMany或HABTM)。 CakePHP 1.x和2.x不支持這個。CakePHP翻譯行爲與相關模型

我的解決方案是非常難看,但工作:

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; 
    } 

也許你有更好的解決方案,你能告訴我:)

回答

2

這是我想出了一個解決方案。

// 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; 
} 
+0

它看起來好多了,我會測試。 – kicaj

+0

此代碼正在爲每個記錄執行SQL查詢,並不好。 –

0

我在我的插件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)沒有翻譯!它在這幾天讓我發瘋。