public function translate($category, $message, $language = null) { $returnedValue = null; if (is_object($category)) { if ($category->isNewRecord || $category->getPrimaryKey() === null) { return null; } if ($category->hasAttribute($message) || property_exists($category, $message)) { $category = '#.' . get_class($category) . '-' . $message . '.' . $category->getPrimaryKey(); $message = null; } else { throw new TsTranslationException('The model ' . get_class($category) . ' have not attribute ' . $message); } } elseif ($category === null) { $category = ''; if (Yii::app()->controller->module !== null) { $category .= Yii::app()->controller->module->id . '.'; } else { $category .= 'root.'; } $category .= Yii::app()->controller->id . '.' . Yii::app()->controller->action->id; } if ($language === null) { $language = Yii::app()->getLanguage(); } if ($this->forceTranslation || $language !== $this->getLanguage() || strpos($category, '#.') === 0) { $returnedValue = $this->translateMessage($category, $message, $language); } else { $returnedValue = $message; } if ($this->ifNotTranslatedShowDefault && $language !== TsTranslationComponent::getDefaultLanguage('code2') && is_null($returnedValue)) { $returnedValue = $this->translate($category, $message, TsTranslationComponent::getDefaultLanguage('code2')); } return !is_null($returnedValue) ? $returnedValue : $this->notTranslatedMessage; }
public static function getSourceColumn($data, $code2) { if (strpos($data->category, '#.') === 0) { echo '<span class="translates-list source-green">' . Yii::t($data->category, $data->message, array(), null, TsTranslationComponent::getDefaultLanguage('code2')) . '</span>'; } elseif (empty($data->message)) { echo '<span class="translates-list source-red">This is empty message</span>'; } else { echo $data->message; } }
/** * Save dynamic content in $language language, * if $language is null uses value returned by TsTranslation::model()->getDtLanguage(). * TsTranslation::model()->getDtLanguage() returns `dynamicTranslate` language, * which select user by widget: * <code> * $this->widget('tstranslation.widgets.TsLanguageWidget', array( * 'dynamicTranslate' => true, * )); * </code> * Example of use: * <code> * if(isset($_POST['Articles'])) { * $model->attributes = $_POST['Articles']; * if($model->save()) { * TsTranslation::save($model, array('title', 'introText', 'fullText')); * } * } * </code> * * @param CModel or string $categoryOrModel * @param string or array $messageOrAttribute * @param string $language * @throws CHttpException if user have not permission * @throws TsTranslationException if CModel $categoryOrModel have not attribute $messageOrAttribute */ public static function save($categoryOrModel, $messageOrAttribute, $language = null) { if (!self::model()->isAccessEnabled()) { throw new CHttpException(403, 'You have no permission to use dynamic content save method!'); } $category = $categoryOrModel; $message = $messageOrAttribute; if ($language === null) { $language = self::model()->getDtLanguage(); } elseif ($language === 'default') { $language = TsTranslationComponent::getDefaultLanguage('code2'); } if (is_object($categoryOrModel)) { if ($categoryOrModel->isNewRecord || $categoryOrModel->getPrimaryKey() === null) { throw new TsTranslationException('The primary key of model ' . get_class($categoryOrModel) . ' is empty. Perhaps you call function TsTranslation::dt() before saving model'); } if (is_array($messageOrAttribute)) { foreach ($messageOrAttribute as $m) { if ($categoryOrModel->hasAttribute($m) || property_exists($categoryOrModel, $m)) { $category = '#.' . get_class($categoryOrModel) . '-' . $m . '.' . $categoryOrModel->getPrimaryKey(); $message = $categoryOrModel->{$m}; self::model()->_save($category, $message, $language); } else { throw new TsTranslationException('The model ' . get_class($categoryOrModel) . ' have not attribute ' . $m); } } } else { if ($categoryOrModel->hasAttribute($messageOrAttribute) || property_exists($categoryOrModel, $messageOrAttribute)) { $category = '#.' . get_class($categoryOrModel) . '-' . $messageOrAttribute . '.' . $categoryOrModel->getPrimaryKey(); $message = $categoryOrModel->{$messageOrAttribute}; self::model()->_save($category, $message, $language); } else { throw new TsTranslationException('The model ' . get_class($categoryOrModel) . ' have not attribute ' . $messageOrAttribute); } } } else { if (!is_string($category)) { throw new TsTranslationException('The first parameter of TsTranslation::save() method must be CActiveRecord model or string'); } if (!is_string($message)) { throw new TsTranslationException('If the first parameter of TsTranslation::save() method is string, the second parameter also must be string'); } $event = new CMissingTranslationEvent(self::getTsTranslation(), $category, $message, $language); self::addTranslation($event); } }
/** * Save dynamic content in $language language, * if $language is null uses value returned by TsTranslation::model()->getDtLanguage(). * TsTranslation::model()->getDtLanguage() returns `dynamicTranslate` language, * which select user by widget: * <code> * $this->widget('tstranslation.widgets.TsLanguageWidget', array( * 'dynamicTranslate' => true, * )); * </code> * Example of use: * <code> * if(isset($_POST['Articles'])) { * $model->attributes = $_POST['Articles']; * if($model->save()) { * TsTranslation::save($model, array('title', 'introText', 'fullText')); * } * } * </code> * * @param CModel or string $categoryOrModel * @param string or array $messageOrAttribute * @param string $language * @throws CHttpException if user have not permission * @throws TsTranslationException if CModel $categoryOrModel have not attribute $messageOrAttribute */ public static function save($categoryOrModel, $messageOrAttribute, $language = null) { if (!self::model()->isAccessEnabled()) { throw new CHttpException(403, 'You have no permission to use dynamic content save method!'); } $category = $categoryOrModel; $message = $messageOrAttribute; if ($language === null) { $language = self::model()->getDtLanguage(); } elseif ($language === 'default') { $language = TsTranslationComponent::getDefaultLanguage('code2'); } if (is_object($categoryOrModel)) { if ($categoryOrModel->isNewRecord || $categoryOrModel->getPrimaryKey() === null) { throw new TsTranslationException('The primary key of model ' . get_class($categoryOrModel) . ' is empty. Perhaps you call function TsTranslation::dt() before saving model'); } if (is_array($messageOrAttribute)) { foreach ($messageOrAttribute as $m) { if (property_exists($categoryOrModel, $m)) { $category = '#.' . get_class($categoryOrModel) . '-' . $m . '.' . $categoryOrModel->getPrimaryKey(); $message = $categoryOrModel->{$m}; self::model()->_save($category, $message, $language); } else { throw new TsTranslationException('The model ' . get_class($categoryOrModel) . ' have not attribute ' . $m); } } } else { if (property_exists($categoryOrModel, $messageOrAttribute)) { $category = '#.' . get_class($categoryOrModel) . '-' . $messageOrAttribute . '.' . $categoryOrModel->getPrimaryKey(); $message = $categoryOrModel->{$messageOrAttribute}; self::model()->_save($category, $message, $language); } else { throw new TsTranslationException('The model ' . get_class($categoryOrModel) . ' have not attribute ' . $messageOrAttribute); } } } else { self::model()->_save($category, $message, $language); } }