It would be great, if Yii has a translate behavior like CakePHP, described here:
It would be great, if Yii has a translate behavior like CakePHP, described here:
yes that be good
wait for this feature too
I need this feature too. Thanks.
This should be very useful also for me, thank you
1
Simple Translate behavior for use with CDbMessageSource:
<?php class Translatebehavior extends CActiveRecordbehavior { private $_beforeUpdateMessageValue; public $category; public $messageField; public function afterFind($event) { $this->_beforeUpdateMessageValue = $this->owner->{$this->messageField}; } public function afterSave($event) { if ($this->owner->isNewRecord) { $sourceMessage = new SourceMessage; $sourceMessage->category = $this->category; $sourceMessage->message = $this->owner->{$this->messageField}; $sourceMessage->save(); } else SourceMessage::model()->updateAll(array('message' => $this->owner->{$this->messageField}), "category LIKE '{$this->category}' AND message LIKE '{$this->_beforeUpdateMessageValue}'"); } public function beforeDelete($event) { SourceMessage::model()->deleteAll("category LIKE '{$this->category}' AND message LIKE '{$this->owner->{$this->messageField}}'"); } }
SourceMessage - simple AR Model
<?php class SourceMessage extends CActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } }
How to use:
<?php class EntityType extends CActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } public function behaviors() { return array( 'Translatebehavior' => array( 'class' => 'Translatebehavior', 'category' => 'entityType', 'messageField' => 'title', ), ); } }
@crecoder: as long as Yii doens't provide a build-in solution I've handled it also with a behavior. But where is the advantage of using "SourceMessage"?
I've done this without using "SourceMessage".
Advanced Translate behavior:
<?php class Translatebehavior extends CActiveRecordbehavior { private $_afterFindMessageValue; public $category; public $messageField = 'title'; public function afterFind($event) { $this->_afterFindMessageValue = $this->owner->{$this->messageField}; } public function afterSave($event) { $sourceMessageCount = SourceMessage::model()->count( 'category LIKE :category AND message LIKE :message', array( ':category' => $this->category, ':message' => $this->owner->{$this->messageField}, ) ); if ($this->owner->getIsNewRecord()) { if (!$sourceMessageCount) $this->createSourceMessage(); } else { if ($sourceMessageCount) { if (!$this->countOwnerMessageFieldRows($this->_afterFindMessageValue)) $this->deleteSourceMessage($this->_afterFindMessageValue); } else { if ($this->countOwnerMessageFieldRows($this->_afterFindMessageValue)) $this->createSourceMessage(); else SourceMessage::model()->updateAll( array('message' => $this->owner->{$this->messageField}), 'category LIKE :category AND message LIKE :message', array( ':category' => $this->category, ':message' => $this->_afterFindMessageValue, ) ); } } } public function beforeDelete($event) { if (1 == $this->countOwnerMessageFieldRows($this->owner->{$this->messageField})) $this->deleteSourceMessage($this->owner->{$this->messageField}); } private function countOwnerMessageFieldRows($value) { return $this->owner->count($this->messageField.' LIKE :messageField', array(':messageField' => $value)); } private function createSourceMessage() { $sourceMessage = new SourceMessage; $sourceMessage->category = $this->category; $sourceMessage->message = $this->owner->{$this->messageField}; $sourceMessage->save(); } private function deleteSourceMessage($value) { SourceMessage::model()->deleteAll( 'category LIKE :category AND message LIKE :message', array( ':category' => $this->category, ':message' => $value, ) ); } }
I would like If feature like Cake's or Symfony's i18n will be built-in into Yii natively.
creocoder, I have a question: Why you have used “LIKE” in sql condition?
Because message is TEXT field, but we can use SQL '='.