/** * @service language add * @return Gpf_Rpc_Form */ public function add(Gpf_Rpc_Params $params) { $form = new Gpf_Rpc_Form($params); $translation = new Gpf_Lang_Parser_Translation(); $languageId = ''; //load fields to translation and languageId $fields = new Gpf_Data_RecordSet(); $fields->loadFromArray($params->get("fields")); foreach ($fields as $field) { switch ($field->get('name')) { case 'translation': $translation->setDestinationMessage($field->get("value")); break; case 'source': $translation->setSourceMessage($field->get("value")); break; case 'languageid': $languageId = $field->get("value"); default: break; } } $language = new Gpf_Db_Language(); $language->setId($languageId); $language->load(); $csvFile = new Gpf_Io_Csv_Reader(Gpf_Lang_CsvLanguage::getAccountCsvFileName($language->getCode())); $csvLanguage = new Gpf_Lang_CsvLanguage(); $csvLanguage->loadFromCsvFile($csvFile); if ($csvLanguage->existTranslation($translation)) { $form->setErrorMessage($this->_('Translation already exist! You can not add source message multiple times.')); return $form; } else { $translation->setCustomerSpecific(true); $translation->setStatus(Gpf_Lang_Parser_Translation::STATUS_TRANSLATED); $translation->setType(Gpf_Lang_Parser_Translation::TYPE_BOTH); $csvLanguage->addTranslation($translation); } $csvLanguage->exportAccountCache(); $language->setTranslatedPercentage($csvLanguage->getTranslationPercentage()); $language->save(); $form->setInfoMessage($this->_("%s was successfully added", $this->_('translation'))); return $form; }
/** * Compute translation array for given language code * * @param string $langCode * @return Gpf_Lang_CsvLanguage */ protected function computeLanguage($langCode) { $file = new Gpf_Io_Csv_Reader(Gpf_Paths::getInstance()->getLanguageInstallDirectory() . Gpf_Application::getInstance()->getCode() . '_' . $langCode . '.csv', ';', '"', array('source', 'translation', 'type', 'module', 'status', 'customer')); $language = new Gpf_Lang_CsvLanguage(); $language->loadFromCsvFile($file); //check if translation is not depreceated foreach ($language->getTranslations() as $translation) { if (!isset($this->sourceTranslations[$translation->getId()])) { $translation->setStatus(Gpf_Lang_Parser_Translation::STATUS_DEPRECATED); } } //add new translations, set translation modules foreach ($this->sourceTranslations as $sourceTranslation) { try { //try if it is existing translation $translation = $language->getTranslation($sourceTranslation, false); $translation->setModules($sourceTranslation->getModules()); $translation->setType($sourceTranslation->getType()); if ($translation->getStatus() == Gpf_Lang_Parser_Translation::STATUS_DEPRECATED) { $translation->setStatus(Gpf_Lang_Parser_Translation::STATUS_NOT_TRANSLATED); } if ($translation->getStatus() == Gpf_Lang_Parser_Translation::STATUS_NOT_TRANSLATED) { try { //check if in reference language is this translation same or not - maybe it is already translated if (is_object($this->referenceLanguage)) { $refTranslation = $this->referenceLanguage->getTranslation($translation); if ($translation->getDestinationMessage() != $translation->getSourceMessage() && $translation->getDestinationMessage() != $refTranslation->getDestinationMessage()) { //destination message is unique, set it as translated $translation->setStatus(Gpf_Lang_Parser_Translation::STATUS_TRANSLATED); } } } catch (Exception $e) { } } } catch (Exception $e) { //this is new translation $translation = clone $sourceTranslation; $translation->setStatus(Gpf_Lang_Parser_Translation::STATUS_NOT_TRANSLATED); try { //load translation from reference translation if exist if (is_object($this->referenceLanguage)) { $refTranslation = $this->referenceLanguage->getTranslation($translation); $translation->setDestinationMessage($refTranslation->getDestinationMessage()); } else { throw new Gpf_Exception('Reference language not defined yet'); } } catch (Gpf_Exception $e) { $translation->setDestinationMessage($translation->getSourceMessage()); } $language->addTranslation($translation); } } return $language; }
/** * If exists language with this code in account directory, load custom translations * * @param $language */ private function loadCustomTranslations(Gpf_Lang_CsvLanguage $language) { $origFileName = Gpf_Lang_CsvLanguage::getAccountCsvFileName($language->getCode()); $file = new Gpf_Io_File($origFileName); if ($file->isExists()) { $file = new Gpf_Io_Csv_Reader($origFileName); foreach ($file as $record) { switch ($record->get('type')) { case 'M': //Metadata - no processing required break; case '': //empty row - no processing required break; default: try { $translation = new Gpf_Lang_Parser_Translation(); $translation->loadFromRecord($record); if (!$language->existTranslation($translation) && $translation->isCustomerSpecific() == Gpf::YES) { //add missing customer translation from current account language $language->addTranslation($translation); } else { //replace custom translation with own text even if it was already in file $existingTranslation = $language->getTranslation($translation); if ($translation->isCustomerSpecific() == Gpf::YES) { //keep custom translation from existing language file ! $existingTranslation->setDestinationMessage($translation->getDestinationMessage()); $existingTranslation->setCustomerSpecific($translation->isCustomerSpecific()); } } } catch (Exception $e) { } break; } } $file->close(); } }