Skip to content

Instantly share code, notes, and snippets.

Created July 17, 2017 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/37dd1b9202c7b0c5d67d9b24ca624e7c to your computer and use it in GitHub Desktop.
Save anonymous/37dd1b9202c7b0c5d67d9b24ca624e7c to your computer and use it in GitHub Desktop.
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