/** * 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; }
/** * Adds new message source to the database * * @param String $category Category of the source message * @param String $source The source message */ public static function addNewSource($category, $source) { assert('is_string($category) && !empty($category)'); assert('is_string($source) && !empty($source)'); $model = new MessageSource(); $model->category = $category; $model->source = $source; if (!$model->save()) { throw new FailedToSaveModelException(); } return $model; }
/** * 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; }