1

Temat: i18n behavior

Obecnie kod wygląda tak

<?php
class I18nBehavior extends ModelBehavior{

    public $fields = array();

    public $defaultLanguage = null;

    public function setup(&$model, $config = array()){
        $this->defaultLanguage = Configure::read('App.defaultLanguage');
        if( !$this->defaultLanguage ){
            trigger_error("Add to core.php line: Configure::write('App.defaultLanguage', 'pol');");
        }
        if( !empty($config['fields']) ){
            $this->fields[$model->alias] = $config['fields'];
        }
    }

    public function beforeFind(&$model, $query){
        $locale = $this->getLocale($model);
        $recursive = empty($query['recursive']) ? 0 : $query['recursive'];
        $this->_localizeScheme($model, $locale, $recursive);
        $this->localizeQuery($model, $query, $locale, $recursive);
        return $query;
    }

    protected function localize(&$array, $alias, $locale){
        foreach($this->fields[$alias] as $field){
            if( !is_array($array) ) return false;
            foreach($array as $index => $value){
                $indexReplace = false;
                if( (string)$index == $alias.'.'.$field ){
                    $indexReplace = true;
                    $replace = $alias.'.'.$field.'_'.$locale;
                }
                if( (string)$index == $field ){
                    $indexReplace = true;
                    $replace = $field.'_'.$locale;
                }
                if( $indexReplace ){
                    $array[$replace] = $value;
                    unset($array[$index]);
                    $index = $replace;
                }
                if( is_array($value) ){
                    $this->localize($array[$index], $alias, $locale);
                }else{
                    if(substr($value,-(1+strlen($locale))) !== '_'.$locale){
                        $array[$index] = str_replace($field, $field.'_'.$locale, $array[$index]);
                    }
                }
            }
        }
    }

    protected function localizeQuery(&$model, &$query, $locale, $recusrsive){
        if( isset($model->Behaviors->I18n) && isset($model->Behaviors->I18n->fields[$model->alias]) ){
            foreach(array('fields', 'conditions', 'order', 'contain') as $part){
                $this->localize($query[$part], $model->alias, $locale);
            }
        }
    }

    protected function getLocale(&$model) {
        if(!isset($model->locale) || is_null($model->locale)) {
            if(!ClassRegistry::isKeySet('I18n')) {
                App::Import('Core', 'i18n');
            }
            $I18n =& I18n::getInstance();
            $model->locale = $I18n->l10n->locale;
        }
        return $model->locale;
    }

}
?>

Teraz tak, powiedzmy ze mam dwa modele : Offer i RoomOFfer. Offer HABTM RoomOffer HABTM Offer.
Tak więc tabele przykladowo wyglądają tak
Offer
- id
- object_description_pol
- object_description_eng

RoomFeatore
- id
- name_pol
- name_eng

Przykladowa akcja w kontrolerze wygląda tak:

public function test(){
    $this->Offer->find('all', array(
        'order' => array('Offer.object_description' => 'DESC'),
        'fields' => array('object_description'),
        'contain' => array('RoomFeature' => array('fields' => 'name'))
    ));

}

Konfiguracja modeli :
Offer

public $actsAs = array('I18n' => array('fields' => array('object_description', 'short_object_description')));

RoomFeature

public $actsAs = array('I18n' => array('fields' => array('name')));

Problem jest taki, że Behavior prawidłowo zmienia nazwy wszystkich zdefiniowanych kolumn dla modelu Offer ale tak jakby nie działała dla modelu RoomFeature, tzn dodastaje błąd że kolumna name w tabeli rooms_features nie istnieje ( co się zgadza ).
Wydaje mi się że muszę w metodzie localizeQuery dodać coś w tym stylu

if (isset($model->hasAndBelongsToMany)) {
    foreach ($model->hasAndBelongsToMany as $name => &$relation) {
        if (isset($model->$name->Behaviors->I18n)) {
            // NIE WIEM CO TUTAJ WSTAWIĆ
        }
    }
}

Ma ktoś jakiś pomysł jak 'odpalić' ten behavior dla wszystkich modeli które zdefiniowane są w 'query' ?