7
CakePHP Translate Behavior with associated templates
source link: https://www.codesd.com/item/cakephp-translate-behavior-with-associated-templates.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
CakePHP Translate Behavior with associated templates
advertisements
I looking-for better solution for using Translate Behavior/i18n for related models (hasOne, hasMany or HABTM). CakePHP 1.x and 2.x not support this.
My solution is very ugly, but work:
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;
}
Maybe You have better solution and can You show me:)
This is the solution I came up with.
// 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;
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK