/**
  * 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;
 }
Esempio n. 2
0
 /**
  * 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;
 }