public function regenerateLanguageCacheFiles()
 {
     //load langage
     $dbLang = new Gpf_Db_Language();
     $dbLang->setAccountId(Gpf_Session::getInstance()->getAuthUser()->getAccountId());
     $dbLang->setCode($this->languageCode);
     $dbLang->setId($dbLang->generateId());
     $dbLang->load();
     $lang = new Gpf_Lang_CsvLanguage();
     $lang->loadFromCsvFile(new Gpf_Io_Csv_Reader(Gpf_Lang_CsvLanguage::getAccountCsvFileName($this->languageCode)));
     $lang->exportAccountCache();
 }
 protected function execute()
 {
     $lang = $this->loadLanguage();
     $file = new Gpf_Io_Csv_Reader($this->fileName, ';', '"', array('source', 'translation', 'type', 'module', 'status', 'customer'));
     $this->language = new Gpf_Lang_CsvLanguage();
     $this->language->loadFromCsvFile($file);
     $pendingMessage = $this->_('Importing language dictionary %s (%s)', $this->language->getMetaValue(Gpf_Lang_CsvLanguage::LANG_NAME), $this->language->getMetaValue(Gpf_Lang_CsvLanguage::LANG_ENG_NAME));
     if ($this->saveMetadata) {
         if ($this->isPending('saveMetadata', $pendingMessage)) {
             $this->saveLanguageMetadata($lang);
             $this->setDone();
         }
     }
     if ($this->isPending('loadCustomTranslations', $pendingMessage)) {
         $this->loadCustomTranslations($this->language);
         $this->setDone();
     }
     if ($this->isPending('backupOriginalTranslation', $pendingMessage)) {
         $this->backupOriginalTranslation($this->language);
         $this->setDone();
     }
     if ($this->isPending('exportCache', $this->_('Exporting dictionary cache.'))) {
         $this->language->exportAccountCache();
         $this->setDone();
     }
 }
 /**
  * @service language write
  * @return Gpf_Rpc_Form
  */
 public function save(Gpf_Rpc_Params $params)
 {
     $form = new Gpf_Rpc_Form($params);
     $result = parent::save($params);
     Gpf_Lang_CsvLanguage::rebuildLanguageCache($form->getFieldValue('Id'));
     return $result;
 }
 /**
  * Download cached csv language file
  *
  * @service language export
  * @param Gpf_Rpc_Params $params
  */
 public function downloadCsvLanguage(Gpf_Rpc_Params $params)
 {
     $form = new Gpf_Rpc_Form($params);
     $download = new Gpf_File_Download_FileSystem(Gpf_Lang_CsvLanguage::getAccountCsvFileName($form->getFieldValue('code')));
     $download->setAttachment(true);
     return $download;
 }
 /**
  * @service language read
  * @return Gpf_Rpc_Serializable
  */
 public function getRows(Gpf_Rpc_Params $params)
 {
     $language = new Gpf_Db_Language();
     $language->setId($params->get(Gpf_Db_Table_Languages::ID));
     $language->load();
     $filters = $params->get('filters');
     $filters[0][2] = htmlspecialchars($filters[0][2]);
     $params->set('filters', $filters);
     $this->setCsvReader(new Gpf_Io_Csv_Reader(Gpf_Lang_CsvLanguage::getAccountCsvFileName($language->getCode())));
     return parent::getRows($params);
 }
 /**
  * Create new language
  *
  * @service language add
  * @return Gpf_Rpc_Form
  */
 public function add(Gpf_Rpc_Params $params)
 {
     $form = new Gpf_Rpc_Form($params);
     $lang = new Gpf_Db_Language();
     $lang->setCode($form->getFieldValue(Gpf_Db_Table_Languages::CODE));
     $lang->setAccountId(Gpf_Application::getInstance()->getAccountId());
     $lang->setId($lang->generateId());
     try {
         //Load should fail, otherwise it is error - language already exists
         $lang->load();
         $form->setErrorMessage($this->_('Language code %s is already used in your installation', $form->getFieldValue(Gpf_Db_Table_Languages::CODE)));
         return $form;
     } catch (Exception $e) {
     }
     try {
         //Load language from default csv file
         $fileNameDefault = Gpf_Paths::getInstance()->getLanguageInstallDirectory() . Gpf_Application::getInstance()->getCode() . '_' . Gpf_Lang_CsvHandler::DEFAULT_LANGUAGE . '.csv';
         $file = new Gpf_Io_Csv_Reader($fileNameDefault, ';', '"', array('source', 'translation', 'type', 'module', 'status', 'customer'));
         $csvLanguage = new Gpf_Lang_CsvLanguage();
         $csvLanguage->loadFromCsvFile($file);
         $form->fill($lang);
         $lang->setAccountId(Gpf_Application::getInstance()->getAccountId());
         $lang->setId($lang->generateId());
         $lang->setActive(true);
         $lang->setTranslatedPercentage(0);
         $lang->insert();
         //update metadata
         $csvLanguage->copyMetadataFromDbLanguage($lang);
         foreach ($csvLanguage->getTranslations() as $translation) {
             $translation->setStatus(Gpf_Lang_Parser_Translation::STATUS_NOT_TRANSLATED);
         }
         //export cache
         $csvLanguage->exportAccountCache();
     } catch (Exception $e) {
         $form->setErrorMessage($this->_('Failed to create new language: %s', $e->getMessage()));
         return $form;
     }
     $form->setInfoMessage($this->_('New language with code %s created', $form->getFieldValue(Gpf_Db_Table_Languages::CODE)));
     return $form;
 }
 public function recomputeTranslationPercentage($languageId)
 {
     $lang = new Gpf_Db_Language();
     $lang->setId($languageId);
     $lang->load();
     $fileName = Gpf_Lang_CsvLanguage::getAccountCsvFileName($language->getCode());
     $csvLanguage = new Gpf_Lang_CsvLanguage();
     $csvLanguage->loadFromCsvFile(new Gpf_Io_Csv_Reader($fileName));
     $lang->setTranslatedPercentage($csvLanguage->getTranslationPercentage());
     $lang->update(array(self::TRANSLATED_PERCENTAGE));
 }
 /**
  * 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;
 }
 /**
  * Load metadata from imported language
  *
  * @service language import
  * @param Gpf_Rpc_Params $params
  * @return Gpf_Rpc_Form
  */
 public function loadCsvFileMetadata(Gpf_Rpc_Params $params)
 {
     $form = new Gpf_Rpc_Form($params);
     try {
         $file = new Gpf_Io_Csv_Reader($form->getFieldValue("Id"), ';', '"', array('source', 'translation', 'type', 'module', 'status', 'customer'));
         $language = new Gpf_Lang_CsvLanguage();
         $language->loadFromCsvFile($file);
         $form->setField('code', $language->getMetaValue(Gpf_Lang_CsvLanguage::LANG_CODE));
         $form->setField('name', $language->getMetaValue(Gpf_Lang_CsvLanguage::LANG_NAME));
         $form->setField('eng_name', $language->getMetaValue(Gpf_Lang_CsvLanguage::LANG_ENG_NAME));
         $form->setField('author', $language->getMetaValue(Gpf_Lang_CsvLanguage::LANG_AUTHOR));
         $form->setField('version', $language->getMetaValue(Gpf_Lang_CsvLanguage::LANG_VERSION));
         $form->setField('translated', $language->getTranslationPercentage());
     } catch (Exception $e) {
         $form->setErrorMessage($this->_('Failed to import language file. Incorrect file format. (%s)', $e->getMessage()));
     }
     return $form;
 }
Example #10
0
 /**
  * Delete csv file from account directory
  */
 private function deleteLanguageFilesFromAccount()
 {
     //delete csv file from account
     $fileName = Gpf_Lang_CsvLanguage::getAccountCsvFileName($this->getCode());
     $file = new Gpf_Io_File($fileName);
     if ($file->isExists()) {
         $file->delete();
     }
     //TODO delete also cache language files from account
 }
 /**
  * @service language write
  * @return Gpf_Rpc_Action
  */
 public function saveFields(Gpf_Rpc_Params $params)
 {
     $action = new Gpf_Rpc_Action($params);
     $action->setErrorMessage($this->_('Failed to save %s field(s)'));
     $action->setInfoMessage($this->_('%s field(s) successfully saved'));
     $language = new Gpf_Db_Language();
     $language->setId($action->getParam(Gpf_Db_Table_Languages::ID));
     $language->load();
     $csvFile = new Gpf_Io_Csv_Reader(Gpf_Lang_CsvLanguage::getAccountCsvFileName($language->getCode()));
     $csvLanguage = new Gpf_Lang_CsvLanguage();
     $csvLanguage->loadFromCsvFile($csvFile);
     $fields = new Gpf_Data_RecordSet();
     $fields->loadFromArray($action->getParam("fields"));
     foreach ($fields as $field) {
         $translation = new Gpf_Lang_Parser_Translation();
         $translation->setSourceMessage($field->get('id'));
         if ($csvLanguage->existTranslation($translation)) {
             $existingTranslation = $csvLanguage->getTranslation($translation);
             if ($existingTranslation->getStatus() == Gpf_Lang_Parser_Translation::STATUS_NOT_TRANSLATED) {
                 $existingTranslation->setStatus(Gpf_Lang_Parser_Translation::STATUS_TRANSLATED);
                 $csvLanguage->incrementTranslatedCount();
             }
             $existingTranslation->set($field->get("name"), $this->sourceCodeSpecialChars($field->get("value")));
         }
         $action->addOk();
     }
     $csvLanguage->exportAccountCache();
     $language->setTranslatedPercentage($csvLanguage->getTranslationPercentage());
     $language->save();
     return $action;
 }
 /**
  * Rebuild cache for language defined by id
  *
  * @param $languageId
  */
 public static function rebuildLanguageCache($languageId)
 {
     $lang = new Gpf_Db_Language();
     $lang->setId($languageId);
     $lang->load();
     $fileName = Gpf_Lang_CsvLanguage::getAccountCsvFileName($lang->getCode());
     $csvLang = new Gpf_Lang_CsvLanguage();
     $csvLang->loadFromCsvFile(new Gpf_Io_Csv_Reader($fileName));
     $csvLang->copyMetadataFromDbLanguage($lang);
     $csvLang->exportAccountCache();
 }