phpcakephptranslate

translate behavior and associated model translation


I searched the web 1000000000000000 times and i can't find a clean solution for this

this is my CertificateType model translation part:

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

and the Certificate model:

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

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

But in fetch time the belonged model will not translate:

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

Why?


Solution

  • I used this and it is working great!

    in AppModel.php I wrote these piece of code:

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