-
-
Save anonymous/37dd1b9202c7b0c5d67d9b24ca624e7c to your computer and use it in GitHub Desktop.
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
function SimpleTranslate ($word) { | |
$timeStamp = [Long]([Double]::Parse((Get-Date -UFormat %s)) * 1000) | |
$appid = "2015063000000001" | |
$key = "12345678" | |
$tohash = $appid + $word + $timeStamp + $key | |
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider | |
$utf8 = new-object -TypeName System.Text.UTF8Encoding | |
$sign = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($tohash))).Replace("-", "").ToLower() | |
$params = @{ | |
q = $word; | |
appid = $appid; | |
salt = $timeStamp; | |
sign = $sign; | |
} | |
$lngCheck = [System.Text.RegularExpressions.Regex]::Match($word, "[\u4e00-\u9fa5]").Success | |
if ($lngCheck) { | |
$params.Add("from", "zh") | |
$params.Add("to", "en") | |
} | |
else { | |
$params.Add("from", "en") | |
$params.Add("to", "zh") | |
} | |
$querystring = "" | |
$params.GetEnumerator() | ForEach-Object {$querystring += $_.Name + "=" + $_.Value + "&"} | |
$url = "http://api.fanyi.baidu.com/api/trans/vip/translate?" + $querystring | |
$rsp = wget $url | ConvertFrom-Json | |
echo $rsp.trans_result[0].dst | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment