/** * Add missing translations to the source table and * If we are using a different translation then the original one * Then add the same message to the translation table. */ public static function load($event) { // Load the messages $source = SourceMessage::model()->find('message=:message AND category=:category', array(':message' => $event->message, ':category' => $event->category)); // If we didn't find one then add it if (!$source) { // Add it $model = new SourceMessage(); $model->category = $event->category; $model->message = $event->message; $model->save(); $lastID = Yii::app()->db->lastInsertID; } /*if( $event->language != Yii::app()->sourceLanguage ) { // Do the same thing with the messages $translation = Message::model()->find('language=:language AND translation=:translation', array(':language'=>$event->language, ':translation'=>$event->message)); // If we didn't find one then add it if( !$translation ) { $source = SourceMessage::model()->find('message=:message AND category=:category', array(':message'=>$event->message, ':category'=>$event->category)); // Add it $model = new Message; $model->id = $source->id; $model->language = $event->language; $model->translation = $event->message; $model->save(); } }*/ }
/** * Import a language * */ public static function importLanguage($filename, $showMessage = true, $fastImport = false) { $localErrors = array(); $allAccounts = array(); $dom = new domDocument(); if (!$dom->load($filename)) { if ($showMessage) { throw new CException(Yii::t('lazy8', 'input file could not be xml parsed')); } else { throw new CException('input file could not be xml parsed'); } } $root = $dom->documentElement; if ($root->nodeName != "lazy8webportlang") { if ($showMessage) { $localErrors = array(array(Yii::t('lazy8', 'Upload failed. This is not a valid file.'), Yii::t('lazy8', 'Select a file and try again'))); } $this->render('importlang'); return $localErrors; } if ($root->getAttribute('version') > 1.0) { if ($showMessage) { $localErrors = array(array(Yii::t('lazy8', 'There maybe problems because this is a file version greater then this programs version'), Yii::t('lazy8', 'Select a file and try again'))); } } $nodeLanguages = $root->getElementsByTagName('language'); unset($root); unset($dom); foreach ($nodeLanguages as $nodeLanguage) { //make sure the company code is unique //Message::model()->dbConnection->createCommand("DELETE FROM Message WHERE language='".$nodeLanguage->getAttribute('langcode')."'")->execute(); //make sure that all the source messages exist for this language if (!$fastImport) { $sources = SourceMessage::model()->findAll(); foreach ($sources as $source) { $foundMessage = Message::model()->find(array('condition' => "language='" . $nodeLanguage->getAttribute('langcode') . "' AND id=" . $source->id)); if ($foundMessage == null) { $trans = new Message(); $trans->id = $source->id; $trans->translation = $source->message; $trans->language = $nodeLanguage->getAttribute('langcode'); $trans->save(); } } } else { //quickly delete all occurances of this language SourceMessage::model()->dbConnection->createCommand("DELETE FROM Message WHERE language='{$nodeLanguage->getAttribute('langcode')}'")->execute(); } //update the version information if ($nodeLanguage->getAttribute('version') != null) { $foundOption = Options::model()->find('name=:name AND userId=:id AND companyId=:compid', array(':name' => "Language.Version." . $nodeLanguage->getAttribute('langcode'), ':id' => 0, ':compid' => 0)); if ($foundOption !== null) { $foundOption->delete(); } $createOption = new Options(); $createOption->name = "Language.Version." . $nodeLanguage->getAttribute('langcode'); $createOption->userId = 0; $createOption->companyId = 0; $createOption->datavalue = $nodeLanguage->getAttribute('version'); $createOption->save(); } //get all the messages $nodeMessages = $nodeLanguage->getElementsByTagName('message'); foreach ($nodeMessages as $nodeMessage) { $sources = SourceMessage::model()->find(array('condition' => "category='" . $nodeMessage->getAttribute('category') . "' AND message='" . SourceMessageController::sqlCleanString(CHtml::encode($nodeMessage->getAttribute('key'))) . "'")); if ($sources == null) { $newSource = new SourceMessage(); $newSource->message = CHtml::encode($nodeMessage->getAttribute('key')); $newSource->category = $nodeMessage->getAttribute('category'); $maxId = 0; $command = SourceMessage::model()->dbConnection->createCommand("SELECT MAX(id) as maxid FROM SourceMessage"); try { $reader = $command->query(); } catch (Exception $e) { echo '<h2>Died on Sql Maxid</h2>'; throw $e; } if ($reader !== null && count($reader) > 0) { foreach ($reader as $row) { $maxId = $row['maxid'] + 1; break; //there is only one row here anyway. } } $newSource->id = $maxId; if ($newSource->save()) { $modelMessage = new Message(); $modelMessage->language = $nodeLanguage->getAttribute('langcode'); $modelMessage->id = $newSource->id; $modelMessage->translation = SourceMessageController::getNodeText($nodeMessage, "translation"); $modelMessage->save(); if (!$fastImport) { //now we must add to all of the other languages. $langs = Message::model()->findAll(array('select' => 'distinct language')); if (isset($langs) && count($langs) > 0) { foreach ($langs as $lang) { if ($nodeLanguage->getAttribute('langcode') != $lang->language) { $newLangItem = new Message(); $newLangItem->id = $newSource->id; $newLangItem->language = $lang->language; $newLangItem->translation = CHtml::encode($nodeMessage->getAttribute('key')); $newLangItem->save(); } } } } } } else { //make sure the key is unique if (!$fastImport) { Message::model()->dbConnection->createCommand("DELETE FROM Message WHERE language='" . $nodeLanguage->getAttribute('langcode') . "' AND id=" . $sources->id)->execute(); } $modelMessage = new Message(); $modelMessage->language = $nodeLanguage->getAttribute('langcode'); $modelMessage->id = $sources->id; $modelMessage->translation = SourceMessageController::getNodeText($nodeMessage, "translation"); //try{ if (!$modelMessage->save()) { if ($showMessage) { $localErrors[] = array(Yii::t('lazy8', 'Failed import of translation.') . ' = ' . $nodeLanguage->getAttribute('langcode') . ' ; ' . $nodeMessage->getAttribute('key'), Yii::t('lazy8', 'Select a file and try again')); } } //}catch(Exception $e){ // echo "error on langcode=" . $nodeLanguage->getAttribute('langcode') . " key=" . $nodeMessage->getAttribute('key'); // die(); //} } } } return $localErrors; }
/** * Import language */ public function actionImport() { // Check access checkAccessThrowException('op_language_import_language'); $file = CUploadedFile::getInstanceByName('file'); $update = getPostParam('update', 0); // Did we upload anything? if (!$file || !$file->getTempName()) { ferror(at('File was not uploaded properly.')); $this->redirect(array('language/index')); } // Make sure it's an xml file if ($file->getType() != 'text/xml') { ferror(at('You must upload an XML file.')); $this->redirect(array('language/index')); } // Make file has contents if (!$file->getSize()) { ferror(at('File Uploaded is empty.')); $this->redirect(array('language/index')); } // Grab data from file $xml = new ClassXML(); $xml->loadXML(file_get_contents($file->getTempName())); // Check to see if it has language details foreach ($xml->fetchElements('language_row') as $lang) { // Grab first language $langData = $xml->fetchElementsFromRecord($lang); break; } // Make sure we have data if (!count($langData)) { ferror(at('Could not locate language data.')); $this->redirect(array('language/index')); } // See if language data missing the name and short form if (!isset($langData['name']) || !isset($langData['abbr'])) { ferror(at('Language data missing name or abbreviation.')); $this->redirect(array('language/index')); } $langName = $langData['name']; $langAbbr = $langData['abbr']; $langId = null; // Check if that language exists $langModel = Language::model()->find('abbr=:abbr', array(':abbr' => $langAbbr)); // If we have the model then set the id if ($langModel) { $langId = $langModel->id; } // Grab the strings $stringsToImport = array(); foreach ($xml->fetchElements('message_row') as $string) { // Grab first language $stringData = $xml->fetchElementsFromRecord($string); $stringsToImport[] = $stringData; } // Make sure we have strings if (!count($stringsToImport)) { ferror(at('Could not locate any strings to import.')); $this->redirect(array('language/index')); } // Do we need to create a new language? if (!$langModel) { // Create new language $newLang = new Language(); $newLang->name = $langName; $newLang->abbr = $langAbbr; if (!$newLang->save()) { ferror(at('Could not save the new language.')); $this->redirect(array('language/index')); } $langId = $newLang->id; } $imported = 0; $updated = 0; $skipped = 0; // Run each string and check if the one exists in the current language if it does and we have the update then update // otherwise skip foreach ($stringsToImport as $r) { // Get orig id if exists if not create orig $orig = SourceMessage::model()->find('category=:category AND message=:message', array(':category' => $r['category'], ':message' => $r['orig'])); if ($orig) { // It exists so we have the original message id $origId = $orig->id; } else { // It does not exists create and get newly created id $newSource = new SourceMessage(); $newSource->category = $r['category']; $newSource->message = $r['orig']; $newSource->save(false); $origId = $newSource->id; } // Now that we have the original id check if we need to update or create $exists = Message::model()->find('id=:id AND language_id=:lang', array(':id' => $origId, ':lang' => $langId)); if ($exists) { if ($update) { // Exists and update $exists->translation = $r['translation']; $exists->update(); $updated++; } else { // Exists do not update $skipped++; } } else { // Does not exist create $newMessage = new Message(); $newMessage->id = $origId; $newMessage->language = $langAbbr; $newMessage->language_id = $langId; $newMessage->translation = $r['translation']; $newMessage->save(false); $imported++; } } // Log and save flash message if ($langModel) { alog(at("Update Language '{name}'", array('{name}' => $langName))); fok(at('Language Updated. {i} Strings Imported, {u} Strings Updated, {s} Strings Skipped.', array('{i}' => $imported, '{u}' => $updated, '{s}' => $skipped))); } else { alog(at("Imported New Language '{name}'", array('{name}' => $langName))); fok(at("New Language Created '{name}'. <b>{i}</b> Strings Imported, <b>{u}</b> Strings Updated, <b>{s}</b> Strings Skipped.", array('{name}' => $langName, '{i}' => $imported, '{u}' => $updated, '{s}' => $skipped))); } $this->redirect(array('language/index')); }
public function afterSave($event) { // do nothing if we have nothing to save if (empty($this->dirtyAttributes)) { return true; } // format into a structured array of translations to save $sourceMessages = array(); foreach ($this->dirtyAttributes as $keyWithSuffix => $value) { $originalAttribute = $this->getOriginalAttribute($keyWithSuffix); $language = $this->getLanguageSuffix($keyWithSuffix); // Do not save translations in sourceLanguage if ($language == Yii::app()->sourceLanguage) { continue; } $sourceMessage = $this->getSourceMessage($originalAttribute); if (!isset($sourceMessages[md5($sourceMessage)])) { $sourceMessages[md5($sourceMessage)] = array('message' => $sourceMessage, 'category' => $this->getCategory($originalAttribute), 'translations' => array()); } $sourceMessages[md5($sourceMessage)]['translations'][] = array('language' => $language, 'translation' => $value); } // do nothing if we have nothing to save if (empty($sourceMessages)) { return true; } // find a suitable method of saving $component = Yii::app()->{$this->messageSourceComponent}; if (method_exists($component, 'saveTranslations')) { $component->saveTranslations($sourceMessages); return $this->afterSavingTranslations(); } if ($component instanceof CPhpMessageSource) { throw new CException("Cannot save translations with CPhpMessageSource"); } if ($component instanceof CDbMessageSource) { // save the translations foreach ($sourceMessages as $sourceMessage) { $attributes = array('category' => $sourceMessage['category'], 'message' => $sourceMessage['message']); if (($model = SourceMessage::model()->find('message=:message AND category=:category', $attributes)) === null) { $model = new SourceMessage(); $model->attributes = $attributes; if (!$model->save()) { throw new CException('Attribute source message ' . $attributes['category'] . ' - ' . $attributes['message'] . ' could not be added to the SourceMessage table. Errors: ' . print_r($model->errors, true)); } } if ($model->id) { foreach ($sourceMessage['translations'] as $translation) { $attributes = array('id' => $model->id, 'language' => $translation['language']); if (($messageModel = Message::model()->find('id=:id AND language=:language', $attributes)) === null) { $messageModel = new Message(); } $messageModel->id = $attributes['id']; $messageModel->language = $attributes['language']; $messageModel->translation = $translation['translation']; if (!$messageModel->save()) { throw new CException('Attribute message ' . $attributes['category'] . ' - ' . $attributes['message'] . ' - ' . $language . ' - ' . $value . ' could not be saved to the Message table. Errors: ' . print_r($messageModel->errors, true)); } } } } return $this->afterSavingTranslations(); } throw new CException("Cannot save translations with " . get_class(Yii::app()->messages)); }