From 8c548a960b159a7a17c0a633b2d2e1959f9a32aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E7=BA=BF=E7=BB=87=E6=AF=9B=E7=90=83?= Date: Sat, 8 Feb 2020 21:57:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A4=9A=E8=AF=AD=E8=A8=80=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/YoudaoTranslate.php | 39 +++++++++++++++++++++++++-------------- src/info.plist | 14 ++++++++------ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/YoudaoTranslate.php b/src/YoudaoTranslate.php index 4c7039d..e278640 100644 --- a/src/YoudaoTranslate.php +++ b/src/YoudaoTranslate.php @@ -46,7 +46,13 @@ public function __construct($keys) */ public function translate($query) { - $this->query = $this->parseCamelPhrase($query); + /** + * @see https://www.php.net/manual/en/function.iconv.php#111909 + * @see https://stackoverflow.com/questions/48537305/same-character-different-length-and-bytes + */ + $query = iconv("UTF-8-MAC", "UTF-8", $query); + + $this->query = $this->isCamelCase($query) ? $this->parseCamelPhrase($query) : $query; $this->queryChinese = $this->isChinese($query); @@ -150,30 +156,35 @@ private function parseError($code) 110 => '无相关服务的有效实例', 111 => '开发者账号无效', 112 => '请求服务无效', + 113 => '查询为空', 202 => '签名检验失败,检查 KEY 和 SCRET', 401 => '账户已经欠费', 411 => '访问频率受限' ]; - return isset($messages[$code]) ? $messages[$code] : '服务异常'; + return isset($messages[$code]) ? $messages[$code] : '翻译失败,错误码:' . $code; } /** - * 检测字符串是否由纯英文,纯中文,中英文混合组成 + * 检查是否为中文 * @param string $str - * @return boolean + * @return false|int */ private function isChinese($str) { - $m = mb_strlen($str, 'utf-8'); - $s = strlen($str); - if ($s == $m) { - return false; - } - if ($s % $m == 0 && $s % 3 == 0) { - return true; - } - return true; + $chinese = '/[\x{4e00}-\x{9fa5}]+/u'; + return preg_match($chinese, $str); + } + + /** + * 检查是不是 CamelCase + * @param string $str + * @return false|int + */ + private function isCamelCase($str) + { + $regex = '/([A-Z]|[a-z])+((\d)|([A-Z0-9][a-z0-9]+))*([A-Z])[a-z]*/'; + return preg_match($regex, $str); } /** @@ -356,7 +367,7 @@ private function getOpenQueryUrl($query) // 有道新版 api 只有当 from 和 to 的值都在{zh-CHS, en}范围内时, // 才有单词字典翻译信息,当两个都是 auto 时则没有 if ($this->queryChinese) { - $key['from'] = 'auto'; + $key['from'] = 'zh-CHS'; $key['to'] = 'en'; } else { $key['from'] = 'auto'; diff --git a/src/info.plist b/src/info.plist index 501a84b..faac5a8 100644 --- a/src/info.plist +++ b/src/info.plist @@ -319,9 +319,11 @@ focusedappvariablename hotkey - 0 + 49 hotmod - 0 + 262144 + hotstring + Space leftcursor modsmode @@ -603,7 +605,9 @@ if (empty($username) || empty($password)) { require('Updater.php'); -$updater = new Updater(getenv('version')); +$version = 'v2.6.0'; + +$updater = new Updater($version); echo $updater->fetchReleases(); @@ -841,8 +845,6 @@ echo $updater->fetchReleases(); netease_username - version - v2.5.0 youdao_appkey youdao_secret @@ -856,7 +858,7 @@ echo $updater->fetchReleases(); youdao_secret version - 2.5.0 + 2.6.0 webaddress https://iwenson.com