private function createEmptyTranslation($locale, $messageId) { // don't create translation if it's just empty if (array_key_exists($messageId, $this->_translate[$locale])) { return; } // no translation found create key if (Pimcore_Tool::isValidLanguage($locale)) { try { $t = Translation_Website::getByKey($messageId); } catch (Exception $e) { $t = new Translation_Website(); $t->setKey($messageId); } $t->addTranslation($locale, ""); $t->save(); } }
public function translationsAction() { $admin = $this->_getParam("admin"); if ($this->getUser()->isAllowed("translations")) { // clear translation cache Pimcore_Model_Cache::clearTags(array("translator", "translate")); if ($this->_getParam("data")) { if ($this->_getParam("xaction") == "destroy") { $key = Zend_Json::decode($this->_getParam("data")); if ($admin) { $t = Translation_Admin::getByKey($key); } else { $t = Translation_Website::getByKey($key); } $t->delete(); $this->_helper->json(array("success" => true, "data" => array())); } else { if ($this->_getParam("xaction") == "update") { $data = Zend_Json::decode($this->_getParam("data")); if ($admin) { $t = Translation_Admin::getByKey($data["key"]); } else { $t = Translation_Website::getByKey($data["key"]); } foreach ($data as $key => $value) { if ($key != "key") { $t->addTranslation($key, $value); } } if ($data["key"]) { $t->setKey($data["key"]); } $t->save(); $return = array_merge(array("key" => $t->getKey(), "date" => $t->getDate()), $t->getTranslations()); $this->_helper->json(array("data" => $return, "success" => true)); } else { if ($this->_getParam("xaction") == "create") { $data = Zend_Json::decode($this->_getParam("data")); if ($admin) { $t = new Translation_Admin(); } else { $t = new Translation_Website(); } $t->setKey($data["key"]); $t->setDate(time()); foreach ($data as $key => $value) { if ($key != "key" && $key != "date") { $t->addTranslation($key, $value); } } $t->save(); $return = array_merge(array("key" => $t->getKey(), "date" => $t->getDate()), $t->getTranslations()); $this->_helper->json(array("data" => $return, "success" => true)); } } } } else { // get list of types if ($admin) { $list = new Translation_Admin_List(); } else { $list = new Translation_Website_List(); } $list->setOrder("asc"); $list->setOrderKey("key"); if ($this->_getParam("dir")) { $list->setOrder($this->_getParam("dir")); } if ($this->_getParam("sort")) { $list->setOrderKey($this->_getParam("sort")); } $list->setLimit($this->_getParam("limit")); $list->setOffset($this->_getParam("start")); if ($this->_getParam("filter")) { $filterTerm = $list->quote("%" . strtolower($this->_getParam("filter")) . "%"); $list->setCondition("lower(`key`) LIKE " . $filterTerm . " OR `text` LIKE " . $filterTerm); } $list->load(); $translations = array(); foreach ($list->getTranslations() as $t) { $translations[] = array_merge(array("key" => $t->getKey(), "date" => $t->getDate()), $t->getTranslations()); } $this->_helper->json(array("data" => $translations, "success" => true, "total" => $list->getTotalCount())); } } else { Logger::err("user [" . $this->getUser()->getId() . "] attempted to access translations, but has no permission to do so."); } $this->_helper->json(false); }
public function translationsImportAction() { $admin = $this->_getParam("admin"); if ($this->getUser()->isAllowed("translations")) { $languages = Pimcore_Tool::getValidLanguages(); try { //read import data $tmpData = file_get_contents($_FILES["Filedata"]["tmp_name"]); //convert to utf-8 if needed $encoding = Pimcore_Tool_Text::detectEncoding($tmpData); if ($encoding) { $tmpData = iconv($encoding, "UTF-8", $tmpData); } //store data for further usage $importFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations"; file_put_contents($importFile, $tmpData); chmod($importFile, 0766); $importFileOriginal = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations_original"; file_put_contents($importFileOriginal, $tmpData); chmod($importFileOriginal, 0766); // determine csv type $dialect = Pimcore_Tool_Admin::determineCsvDialect(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations_original"); //read data if (($handle = fopen(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations", "r")) !== FALSE) { while (($rowData = fgetcsv($handle, 10000, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar)) !== false) { $data[] = $rowData; } fclose($handle); } //process translations if (is_array($data) and count($data) > 1) { $keys = $data[0]; $data = array_slice($data, 1); foreach ($data as $row) { $keyValueArray = array(); for ($counter = 0; $counter < count($row); $counter++) { $rd = str_replace(""", '"', $row[$counter]); $keyValueArray[$keys[$counter]] = $rd; } $t = null; if ($keyValueArray["key"]) { try { if ($admin) { $t = Translation_Admin::getByKey($keyValueArray["key"]); } else { $t = Translation_Website::getByKey($keyValueArray["key"]); } } catch (Exception $e) { Logger::debug("Unable to find translation with key: " . $keyValueArray["key"]); } } if (!$t instanceof Translation_Abstract) { if ($admin) { $t = new Translation_Admin(); } else { $t = new Translation_Website(); } } $t->setDate(time()); foreach ($keyValueArray as $key => $value) { if ($key != "key" && $key != "date" && in_array($key, $languages)) { $t->addTranslation($key, $value); } } if ($keyValueArray["key"]) { $t->setKey($keyValueArray["key"]); } $t->save(); } $this->_helper->json(array("success" => true)); } else { throw new Exception("less than 2 rows of data - nothing to import"); } } catch (Exception $e) { Logger::error($e); $this->_helper->json(false); } } else { Logger::err("user [" . $this->getUser()->getId() . "] attempted to import translations csv, but has no permission to do so."); die; } }