Skip to content

Commit

Permalink
feat: 多语言支持
Browse files Browse the repository at this point in the history
  • Loading branch information
wensonsmith committed Feb 8, 2020
1 parent 7d261ea commit 8c548a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
39 changes: 25 additions & 14 deletions src/YoudaoTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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';
Expand Down
14 changes: 8 additions & 6 deletions src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,11 @@
<key>focusedappvariablename</key>
<string></string>
<key>hotkey</key>
<integer>0</integer>
<integer>49</integer>
<key>hotmod</key>
<integer>0</integer>
<integer>262144</integer>
<key>hotstring</key>
<string>Space</string>
<key>leftcursor</key>
<false/>
<key>modsmode</key>
Expand Down Expand Up @@ -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-&gt;fetchReleases();
Expand Down Expand Up @@ -841,8 +845,6 @@ echo $updater-&gt;fetchReleases();
<string></string>
<key>netease_username</key>
<string></string>
<key>version</key>
<string>v2.5.0</string>
<key>youdao_appkey</key>
<string></string>
<key>youdao_secret</key>
Expand All @@ -856,7 +858,7 @@ echo $updater-&gt;fetchReleases();
<string>youdao_secret</string>
</array>
<key>version</key>
<string>2.5.0</string>
<string>2.6.0</string>
<key>webaddress</key>
<string>https://iwenson.com</string>
</dict>
Expand Down

0 comments on commit 8c548a9

Please sign in to comment.