forked from summerblue/larabbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bfee57
commit d7f9c70
Showing
11 changed files
with
294 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace App\Handlers; | ||
|
||
use GuzzleHttp\Client; | ||
use Overtrue\Pinyin\Pinyin; | ||
|
||
class SlugTranslateHandler | ||
{ | ||
public function translate($text) | ||
{ | ||
// 实例化 HTTP 客户端 | ||
$http = new Client; | ||
|
||
// 初始化配置信息 | ||
$api = 'http://api.fanyi.baidu.com/api/trans/vip/translate?'; | ||
$appid = config('services.baidu_translate.appid'); | ||
$key = config('services.baidu_translate.key'); | ||
$salt = time(); | ||
|
||
// 如果没有配置百度翻译,自动使用兼容的拼音方案 | ||
if (empty($appid) || empty($key)) { | ||
return $this->pinyin($text); | ||
} | ||
|
||
// 根据文档,生成 sign | ||
// http://api.fanyi.baidu.com/api/trans/product/apidoc | ||
// appid+q+salt+密钥 的MD5值 | ||
$sign = md5($appid. $text . $salt . $key); | ||
|
||
// 构建请求参数 | ||
$query = http_build_query([ | ||
"q" => $text, | ||
"from" => "zh", | ||
"to" => "en", | ||
"appid" => $appid, | ||
"salt" => $salt, | ||
"sign" => $sign, | ||
]); | ||
|
||
// 发送 HTTP Get 请求 | ||
$response = $http->get($api.$query); | ||
|
||
$result = json_decode($response->getBody(), true); | ||
|
||
/** | ||
获取结果,如果请求成功,dd($result) 结果如下: | ||
array:3 [▼ | ||
"from" => "zh" | ||
"to" => "en" | ||
"trans_result" => array:1 [▼ | ||
0 => array:2 [▼ | ||
"src" => "XSS 安全漏洞" | ||
"dst" => "XSS security vulnerability" | ||
] | ||
] | ||
] | ||
**/ | ||
|
||
// 尝试获取获取翻译结果 | ||
if (isset($result['trans_result'][0]['dst'])) { | ||
return str_slug($result['trans_result'][0]['dst']); | ||
} else { | ||
// 如果百度翻译没有结果,使用拼音作为后备计划。 | ||
return $this->pinyin($text); | ||
} | ||
} | ||
|
||
public function pinyin($text) | ||
{ | ||
return str_slug(app(Pinyin::class)->permalink($text)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.