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

Vah*_*adi 2 cakephp translate

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

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

public $actsAs = array('Translate'=>array('title','description')) ;
Run Code Online (Sandbox Code Playgroud)

和证书模型:

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

    public $belongsTo = array(
    'CertificateType' => array(
        'className' => 'CertificateType',
        'foreignKey' => 'certificate_type_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ) ,
   );
Run Code Online (Sandbox Code Playgroud)

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

    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()) ;
}
Run Code Online (Sandbox Code Playgroud)

为什么?

Vah*_*adi 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;
}
Run Code Online (Sandbox Code Playgroud)