/** * Creates a message if form has been submitted or renders the creation/update form */ public function actionCreate() { if (isset($_POST['Message'])) { $resp = array('color' => '#FF6600', 'background_color' => '#FFFFCC', 'position' => 'top', 'removebutton' => 0); $model = new Message(); $model->setAttributes($_POST['Message']); if ($model->save()) { $resp['message'] = Yii::t('translate', 'Message successfully saved'); $resp['id'] = $model->id; } else { $errors = $this->errors($model); $resp['message'] = Yii::t('translate', '<p>Unable to save the message. Something went wrong!<p/>' . implode('<br/>', $errors)); } echo $this->je($resp); Yii::app()->end(); } $id = (int) Yii::app()->request->getParam('id'); $lang = Yii::app()->request->getParam('lang'); if ($id && $lang) { $model = MessageSource::model()->findByPk($id); if ($model) { $message = $model->loadMessage(); $message->id = $model->id; $message->language = $lang; $this->renderPartial('form', array('model' => $message)); } else { throw new CHttpException(400); } } else { throw new CHttpException(404, 'The requested page does not exist.'); } }
/** * method that handles the on missing translation event * * @param CMissingTranslationEvent $event * @return string the message to translate or the translated message if autoTranslate is set to true */ function missingTranslation($event) { Yii::import('translate.models.MessageSource'); $attributes = array('category' => $event->category, 'message' => $event->message); if (($model = MessageSource::model()->find('message=:message AND category=:category', $attributes)) === null) { $model = new MessageSource(); $model->attributes = $attributes; if (!$model->save()) { return Yii::log(TranslateModule::t('Message ' . $event->message . ' could not be added to messageSource table')); } } if ($model->id) { if ($this->autoTranslate && substr($event->language, 0, 2) !== substr(Yii::app()->sourceLanguage, 0, 2)) { //&& key_exists($event->language,$this->getGoogleAcceptedLanguages($event->language)) Yii::import('translate.models.Message'); $translation = $this->googleTranslate($event->message, $event->language, Yii::app()->sourceLanguage); $messageModel = new Message(); $messageModel->attributes = array('id' => $model->id, 'language' => $event->language, 'translation' => $translation); if ($messageModel->save()) { $event->message = $translation; } else { return Yii::log(TranslateModule::t('Message ' . $event->message . ' could not be translated with auto-translate')); } } elseif (substr($event->language, 0, 2) !== substr(Yii::app()->sourceLanguage, 0, 2) || Yii::app()->getMessages()->forceTranslation) { self::$messages[$model->id] = array('language' => $event->language, 'message' => $event->message, 'category' => $event->category); } } return $event; }
echo CHtml::script("\r\n \$('#" . TranslateModule::translator()->languageKey . '-pager' . " a').click(function(){\r\n \$dialog=\$('#" . TranslateModule::translator()->languageKey . '-dialog' . "').load(\$(this).attr('href'));\r\n return false;\r\n });\r\n "); } } ?> <div class="form"> <?php echo CHtml::beginForm(); ?> <table> <thead> <th><?php echo MessageSource::model()->getAttributeLabel('category'); ?> </th> <th><?php echo MessageSource::model()->getAttributeLabel('message'); ?> </th> <th><?php echo Message::model()->getAttributeLabel('translation'); ?> </th> <?php echo $google ? CHtml::tag('th') : null; ?> </thead> <tbody> <?php $this->widget('zii.widgets.CListView', array('dataProvider' => new CArrayDataProvider($models), 'pager' => array('id' => TranslateModule::translator()->languageKey . '-pager', 'class' => 'CLinkPager'), 'viewData' => array('messages' => $messages, 'google' => $google), 'itemView' => '_form')); ?> </tbody>
public function attributeLabels() { return array('id' => Yii::t('translate', 'ID'), 'language' => Yii::t('translate', 'Language'), 'translation' => Yii::t('translate', 'Translation'), 'category' => MessageSource::model()->getAttributeLabel('category'), 'message' => MessageSource::model()->getAttributeLabel('message')); }
<?php Yii::app()->controller->pageTitle = TranslateModule::t('Manage Messages'); ?> <?php $source = MessageSource::model()->findAll(); $this->widget('zii.widgets.grid.CGridView', array('id' => 'message-grid', 'dataProvider' => $model->search(), 'filter' => $model, 'columns' => array(array('name' => 'id', 'filter' => CHtml::listData($source, 'id', 'id')), array('name' => 'message', 'filter' => CHtml::listData($source, 'message', 'message')), array('name' => 'category', 'filter' => CHtml::listData($source, 'category', 'category')), array('name' => 'language', 'filter' => CHtml::listData($model->findAll(new CDbCriteria(array('group' => 'language'))), 'language', 'language')), 'translation', array('class' => 'CButtonColumn', 'template' => '{update}', 'buttons' => array('update' => array('label' => t('Edit'), 'imageUrl' => false, 'url' => 'Yii::app()->getController()->createUrl("update",array("id"=>$data->id,"language"=>$data->language))'))), array('class' => 'CButtonColumn', 'template' => '{delete}', 'buttons' => array('delete' => array('label' => t('Delete'), 'imageUrl' => false, 'url' => 'Yii::app()->getController()->createUrl("delete",array("id"=>$data->id,"language"=>$data->language))'))))));
/** * Deletes a record * @param integer $id the ID of the model to be deleted * @param string $language the language of the model to de deleted */ public function actionMissingdelete($id) { if (!Yii::app()->user->isGuest) { if (Yii::app()->getRequest()->getIsPostRequest()) { $model = MessageSource::model()->findByPk($id); if ($model->delete()) { if (Yii::app()->getRequest()->getIsAjaxRequest()) { echo TranslateModule::t('Message deleted successfully'); Yii::app()->end(); } else { $this->redirect(Yii::app()->getRequest()->getUrlReferrer()); } } } else { throw new CHttpException(400); } } else { throw new CHttpException(404, "Error Processing Request"); } }
/** * Method that handles the on missing translation event. If no messagesource is found * then add it to the DB for its future edition from the user's control panel -that is up to you! :). * @param CMissingTranslationEvent $event * @return string the message to translate */ public static function missingTranslation($event) { Yii::import('translate.models.MessageSource'); $attributes = array('category' => $event->category, 'message' => $event->message); if (($model = MessageSource::model()->find('message=:message AND category=:category', $attributes)) === null) { $model = new MessageSource(); $model->attributes = $attributes; if (!$model->save()) { return Yii::log(Yii::t(__CLASS__, 'Message ' . $event->message . ' could not be added to messageSource table')); } } if ($model->id) { if (substr($event->language, 0, 2) !== substr(Yii::app()->sourceLanguage, 0, 2) || Yii::app()->getMessages()->forceTranslation && !array_key_exists($model->id, self::$_missingTranslations)) { Yii::trace('--- ADDED TRANSLATION ---', 'translate'); $url = Yii::app()->getController()->createUrl('/translate/translate/create', array('id' => $model->id, 'lang' => $event->language)); self::$_missingTranslations[$model->id] = array('ref' => $model->id, 'text' => self::c(strip_tags($model->message), 45), 'url' => $url); } } return $event; }