翻译行为和相关的模型翻译

时间:2013-09-12 09:53:30

标签: cakephp translate

我在网上搜索了1000000000000次,但我找不到一个干净的解决方案

这是我的CertificateType模型翻译部分:

public $actsAs = array('Translate'=>array('title','description')) ;

和证书模型:

public $actsAs=array('Translate'=>array('filename')) ;

    public $belongsTo = array(
    'CertificateType' => array(
        'className' => 'CertificateType',
        'foreignKey' => 'certificate_type_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ) ,
   );

但是在获取时间内,所属模型不会翻译:

    public function 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());
    debug($this->Certificate->paginate()) ;
}

为什么?

7 个答案:

答案 0 :(得分:6)

我用过它,效果很好!

在AppModel.php中的

我写了这段代码:

    public function getTranslatedModelField($id = 0, $field) {
    // retrieve active language
    $ActiveLanguageCatalog=CakeSession::read('Config.ActiveLanguageCatalog') ;
    $res = false;
    $translateTable = (isset($this->translateTable))?$this->translateTable:"i18n";

    $db = $this->getDataSource();
    $tmp = $db->fetchAll(
        "SELECT content from {$translateTable} WHERE model = ? AND locale = ? AND foreign_key = ? AND field = ? LIMIT 1",
        array($this->alias, $ActiveLanguageCatalog['locale'], $id, $field)
    );
    if (!empty($tmp)) {
        $res = $tmp[0][$translateTable]['content'];
    }
    return $res;
}   

public function afterFind($results, $primary = false) {

    if($primary == false && array_key_exists('Translate', $this->actsAs)) {
        foreach ($results as $key => $val) {
            if (isset($val[$this->name]) && isset($val[$this->name]['id'])) {
                foreach($this->actsAs['Translate'] as $translationfield) {  
                    $results[$key][$this->name][$translationfield] = $this->getTranslatedModelField($val[$this->name]['id'], $translationfield);
                }
            } else if($key == 'id' && is_numeric($val)) {
                foreach($this->actsAs['Translate'] as $translationfield) {  
                    $results[$translationfield] = $this->getTranslatedModelField($val, $translationfield);
                }                   
            }
         }
    }

    return $results;
}

答案 1 :(得分:1)

将以下代码添加到AppModel:

public function afterFind($results, $primary = false) {
    if(!empty($this->hasMany)) {
        foreach($this->hasMany as $model => $settings) {
            if(isset($this->{$model}->actsAs['Translate'])) {
                if(!empty($results[0][$model])) {
                    foreach($results[0][$model] as $row => $result) {
                        $supplement = $this->{$model}->find('first', array(
                            'conditions' => array(
                                $model .'.id' => $result['id']),
                            'fields' => $this->{$model}->actsAs['Translate'],
                            'recursive' => -1));

                        if(!empty($supplement)) {
                            $results[0][$model][$row] = array_merge($results[0][$model][$row], array_diff($supplement[$model], $result));
                        }
                    }
                }
            }
        }
    }

    if(!empty($this->belongsTo)) {
        foreach($this->belongsTo as $model => $settings) {
            if(isset($this->{$model}->actsAs['Translate'])) {
                if(!empty($results[0][$model])) {
                    foreach($results[0][$model] as $row => $result) {
                        $supplement = $this->{$model}->find('first', array(
                            'conditions' => array(
                                $model .'.id' => $result),
                            'fields' => $this->{$model}->actsAs['Translate'],
                            'recursive' => -1));

                        if(!empty($supplement)) {
                            $results[0][$model] = array_merge($results[0][$model], $supplement[$model]);
                        }
                    }
                }
            }
        }
    }

    return $results;
} 

这适用于关系hasMany和belongsTo

答案 2 :(得分:1)

感谢Vahid Alimohamadi的剪辑。我必须修改它才能在我的项目中正常工作。

public function getTranslatedModelField($id = 0, $field) {
    // retrieve active language
    //$ActiveLanguageCatalog=CakeSession::read('Config.ActiveLanguageCatalog') ;
    $locale = Configure::read('Config.language');
    $res = false;
    $translateTable = (isset($this->translateTable))?$this->translateTable:"i18n";

    $db = $this->getDataSource();
    $tmp = $db->fetchAll(
        "SELECT content from {$translateTable} WHERE model = ? AND locale = ? AND foreign_key = ? AND field = ? LIMIT 1",
        array($this->alias, $locale/*$ActiveLanguageCatalog['locale']*/, $id, $field)
        );
    if (!empty($tmp)) {
        $res = $tmp[0][$translateTable]['content'];
    }
    return $res;
}

public function afterFind($results, $primary = false) {

    if($primary == false && array_key_exists('Translate', $this->actsAs)) {
        foreach ($results as $key => $val) {
            if (isset($val[$this->name]) && isset($val[$this->name]['id'])) {
                //HERE ADDED $field => $translationfield AND PASS IT TO THE getTranslatedModelField
                foreach($this->actsAs['Translate'] as $field => $translationfield) {
                    $results[$key][$this->name][$field] = $this->getTranslatedModelField($val[$this->name]['id'], $field);
                }
            } else if($key == 'id' && is_numeric($val)) {
                //HERE ADDED $field => $translationfield AND PASS IT TO THE getTranslatedModelField
                foreach($this->actsAs['Translate'] as $field => $translationfield) {
                    $results[$field] = $this->getTranslatedModelField($val, $field);
                }
            }
        }
    }

    return $results;
}

答案 3 :(得分:0)

我为获得当前语言做了一些改变,并与蛋糕2.4.3完美配合

public function getTranslatedModelField($id = 0, $field) {
    // retrieve active language

    $locale = $this->locale ;// gets the current assigned locale

    $res = false;
    $translateTable = (isset($this->translateTable))?$this->translateTable:"i18n";

    $db = $this->getDataSource();
    $tmp = $db->fetchAll(
        "SELECT content from {$translateTable} WHERE model = ? AND locale = ? AND foreign_key = ? AND field = ? LIMIT 1",
        array($this->alias, $locale , $id, $field)
    );
    if (!empty($tmp)) {
        $res = $tmp[0][$translateTable]['content'];
    }
    return $res;
}   

public function afterFind($results, $primary = false) {

    if($primary == false && array_key_exists('Translate', $this->actsAs)) {
        foreach ($results as $key => $val) {
            if (isset($val[$this->name]) && isset($val[$this->name]['id'])) {
                foreach($this->actsAs['Translate'] as $translationfield) {  
                    $results[$key][$this->name][$translationfield] = $this->getTranslatedModelField($val[$this->name]['id'], $translationfield);
                }
            } else if($key == 'id' && is_numeric($val)) {
                foreach($this->actsAs['Translate'] as $translationfield) {  
                    $results[$translationfield] = $this->getTranslatedModelField($val, $translationfield);
                }                   
            }
         }
    }

    return $results;
}

答案 4 :(得分:0)

我在cakephp 2.5中有一些更新。它现在为我工作。希望可以提供帮助。

public function getTranslatedModelField($ id = 0,$ field){

    $res = false;
    $translateTable = (isset($this->translateTable))?$this->translateTable:"i18n";

    $db = $this->getDataSource();
    $tmp = $db->fetchAll(
        "SELECT content from {$translateTable} WHERE model = ? AND locale = ? AND foreign_key = ? AND field = ? LIMIT 1",
        array($this->alias, $this->locale , $id, $field)
    );

    if (!empty($tmp)) {
        $res = $tmp[0][$translateTable]['content'];
    }
    return $res;
}

public function afterFind($results, $primary = false) {

    if($primary == false && array_key_exists('Translate', $this->actsAs)) {
        foreach ($results as $key => $val) {
            if (isset($val[$this->name]) && isset($val[$this->name]['id'])) {
                foreach($this->actsAs['Translate'] as $k =>  $translationfield) {
                    $results[$key][$this->name][$translationfield] = $this->getTranslatedModelField($val[$this->name]['id'], $k);
                }
            } else if($key == 'id' && is_numeric($val)) {
                foreach($this->actsAs['Translate'] as $k =>  $translationfield) {
                    $results[$translationfield] = $this->getTranslatedModelField($val, $k);
                }
            }
        }
    }

    return $results;
}

答案 5 :(得分:-1)

最简单的解决方案是获取相关模型项的ID,并进行如下搜索:

            $ids = array();
    foreach ($wpage['Widget'] as &$widget):
        $ids[] = $widget['id'];
    endforeach;

    $widgets = $this->Wpage->Widget->find('all', array(
        'conditions' => array('Widget.id' => $ids)
    ));

    $this->set(compact('wpage', 'widgets'));    

谢谢, ARIS

答案 6 :(得分:-1)

通过使用Model-> SubModel-> Behaviors-> load(...)

,以Cake方式启用相关模型的翻译行为非常简单
$this->Certificate->CertificateType->Behaviors->load(
    'Translate',
     array('title','description')
     );

查看http://book.cakephp.org/2.0/en/models/behaviors.html