/** * Singleton instance * * @return array */ public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; }
/** * 中文转拼音或者英文 * */ public function zh2pinyin() { import("@.ORG.pinyin"); $pinyin = new pinyin(); if ($_REQUEST['type'] == 'pinyin') { echo $pinyin->zh($_REQUEST['text']); } elseif ($_REQUEST['type'] == 'pinyin_first_letter') { echo $pinyin->first_letter($_REQUEST['text']); } else { import("@.ORG.GoogleTranslate"); $GoogleTranslate = new GoogleTranslate(); echo $GoogleTranslate->code($_REQUEST['text']); } }
/** * ajax方式检查应用是否存在 * */ public function check_module() { $title = $this->_get('title'); $module_name = $this->_get('module_name'); if ($_REQUEST['type'] == 'pinyin') { import('@.ORG.pinyin'); $pinyin = new pinyin(); if ($title) { $module_name = $pinyin->zh($title); } else { $module_name = $pinyin->zh($module_name); } } else { import('@.ORG.GoogleTranslate'); $GoogleTranslate = new GoogleTranslate(); if ($title) { $module_name = $GoogleTranslate->code($title); } else { $module_name = $GoogleTranslate->code($module_name); } } $db_node = M('node'); $count = $db_node->where("name='{$module_name}' AND level=2")->count(); if ($count < 1) { $match = eregi("^[A-Za-z][A-Za-z0-9_]+\$", $module_name); if ($match) { $module_name = ucfirst($module_name); $this->ajaxReturn($module_name, '输入正确', 1); } else { $this->ajaxReturn('', '只能输入字母、数字和_,第一个字符必需是字母。', 2); } } else { $this->ajaxReturn('', '应用已存在,请重新输入', 3); } }
<?php /* * Colocamos los header necesario para habilitar peticiones GET y POST * y también le decimos que el tipo de contenido es JSON. * */ header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST'); header('Content-Type: application/json'); /* * Importamos la clase * https://github.com/statickidz/Google-Translator-Free-PHP */ require_once 'GoogleTranslate.class.php'; /* * Para instanciarla la llamamos con dos valores * source: lenguaje de origen * target: lenguaje de destino * Seguidamente usamos el método translate($string) para traducir */ $translator = new GoogleTranslate($_GET['source'], $_GET['target']); $translation = $translator->translate($_GET['q']); $response = new stdClass(); if ($translation != '') { $response->status = true; $response->translation = $translation; } else { $response->status = false; } echo json_encode($response);
return $this->lastResult = self::staticTranslate($string, $this->langFrom, $this->langTo); } /** * Static method for translating text * * @param string $string Text to translate * @param string $from Language code * @param string $to Language code * @return string/boolean Translated text * @access public */ public static function staticTranslate($string, $from, $to) { $url = sprintf(self::$urlFormat, rawurlencode($string), $from, $to); $result = preg_replace('!,+!', ',', self::makeCurl($url)); // remove repeated commas (causing JSON syntax error) $resultArray = json_decode($result, true); $finalResult = ""; if (!empty($resultArray[0])) { foreach ($resultArray[0] as $results) { $finalResult .= $results[0]; } return $finalResult; } return false; } } print GoogleTranslate::staticTranslate($argv[1], $argv[2], $argv[3]); /*$handle = fopen($argv[1], "r"); if ($handle) { while (($line = fgets($handle)) !== false) {*/
/** * Perform the importing of words * * @access private * @param array $_post _POST array * @return void */ private function importing($_post) { $msg = null; // check permission $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], '_key_import', 0, 4); if (is_null($msg)) { // get key list($lang, $area, $what) = explode('-', $_post['what']); // handle _post $post = array('lang' => $_post['lang'], 'area' => $_post['area'], 'what' => $what, 'xon' => 1); // set the translator X4Core_core::auto_load('google_translate_library'); $translator = new GoogleTranslate($lang, $post['lang']); // get words to import $dict = new Dictionary_model(); if ($what == 'ALL') { // import all sections in an area $sections = $dict->get_sections($lang, $area); $result = true; foreach ($sections as $s) { // get words in section $words = $dict->get_words_to_import($lang, $area, $s->what, $post['lang'], $post['area']); if (!empty($words)) { $post['what'] = $s->what; // import foreach ($words as $i) { $post['xkey'] = $i->xkey; // try to translate if ($lang != $post['lang']) { $value = $translator->translate($i->xval); } else { $value = $i->xval; } // set the word $post['xval'] = $value; // insert $result = $dict->insert($post); // add permission if ($result[1]) { $amod = new Area_model(); $id_area = $amod->get_area_id($_post['area']); $perm = new Permission_model(); $array[] = array('action' => 'insert', 'id_what' => $result[0], 'id_user' => $_SESSION['xuid'], 'level' => 4); $res = $perm->pexec('dictionary', $array, $id_area); } } } } // set what for redirect $what = 'global'; } else { // import only one section $words = $dict->get_words_to_import($lang, $area, $what, $post['lang'], $post['area']); $result = true; // import foreach ($words as $i) { $post['xkey'] = $i->xkey; // try to translate if ($lang != $post['lang']) { $value = $translator->translate($i->xval); } else { $value = $i->xval; } // set the word $post['xval'] = $value; // insert $result = $dict->insert($post); // add permission if ($result[1]) { $amod = new Area_model(); $id_area = $amod->get_area_id($_post['area']); $perm = new Permission_model(); $array[] = array('action' => 'insert', 'id_what' => $result[0], 'id_user' => $_SESSION['xuid'], 'level' => 4); $res = $perm->pexec('dictionary', $array, $id_area); } } } $msg = AdmUtils_helper::set_msg($result); // set what update if ($result[1]) { $msg->update[] = array('element' => 'tdown', 'url' => BASE_URL . 'dictionary/keys/' . $post['lang'] . '/' . $post['area'] . '/' . $what, 'title' => null); } } $this->response($msg); }