示例#1
0
 public function translate($key)
 {
     $translated = $key;
     if ($key) {
         $locale = $_REQUEST["systemLocale"];
         if ($locale) {
             try {
                 $translation = Translation_Admin::getByKey($key);
             } catch (Exception $e) {
             }
             if ($translation instanceof Translation_Admin) {
                 if ($translation->getTranslation($locale)) {
                     $translated = $translation->getTranslation($locale);
                 } else {
                     if (PIMCORE_DEBUG) {
                         $translated = "+" . $key . "+";
                     }
                 }
             } else {
                 $t = new Translation_Admin();
                 $availableLanguages = Pimcore_Tool_Admin::getLanguages();
                 $t->setKey($key);
                 $t->setDate(time());
                 foreach ($availableLanguages as $lang) {
                     $t->addTranslation($lang, "");
                 }
                 try {
                     $t->save();
                 } catch (Exception $e) {
                     Logger::debug(get_class($this), ": could not save new translation for key [ {$key} ]");
                 }
             }
         }
     }
     return $translated;
 }
示例#2
0
 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 addAdminTranslationKeysAction()
 {
     $this->removeViewRenderer();
     $keys = $this->_getParam("keys");
     if ($keys) {
         $availableLanguages = Pimcore_Tool_Admin::getLanguages();
         $data = Zend_Json_Decoder::decode($keys);
         foreach ($data as $translationData) {
             $t = null;
             // reset
             try {
                 $t = Translation_Admin::getByKey($translationData);
             } catch (Exception $e) {
                 Logger::log($e);
             }
             if (!$t instanceof Translation_Admin) {
                 $t = new Translation_Admin();
                 $t->setKey($translationData);
                 $t->setDate(time());
                 foreach ($availableLanguages as $lang) {
                     $t->addTranslation($lang, "");
                 }
                 try {
                     $t->save();
                 } catch (Exception $e) {
                     Logger::log($e);
                 }
             }
         }
     }
 }