复制代码
- class QQTranslator {
- # =========================================================================
- # 成员变量
- # =========================================================================
- $secretId = ""
- $secretKey = ""
- $projectId = 0
- $sourceLanguage = "auto"
- $targetLanguage = "zh"
- $region = "ap-hongkong"
- # =========================================================================
- # 公共成员函数
- # =========================================================================
- # 设置 secret Id
- function setSecretId($id)
- {
- $this.secretId = $id
- }
- # 设置 secret key
- function setSecretKey($key)
- {
- $this.secretKey = $key
- }
- # 设置 project id
- function setProjectId($id)
- {
- $this.projectId = $id
- }
- # 指定源语言
- function setSourceLanguage($str)
- {
- $this.sourceLanguage = $str
- }
- # 指定目标语言
- function setTargetLanguage($str)
- {
- $this.targetLanguage = $str
- }
- # 设置所在地域的服务器
- function setRegion($str)
- {
- $this.region = $str
- }
- # 一次翻译一个句子
- function translateString($sourceText)
- {
- $secretId = $this.secretId
- $secretKey = $this.secretKey
- $projectId = $this.projectId
- $region = $this.region
- $sourceLanguage = ""
- $targetLanguage = ""
- # 取得传入参数的数组
- $count = $args.Length
- switch ($count)
- {
- 1 {
- # 用户未指定源语言和目标语言
- $sourceLanguage = $this.sourceLanguage
- $targetLanguage = $this.targetLanguage
- }
- 2 {
- # 用户未指定目标语言
- $sourceLanguage = $this.sourceLanguage
- $targetLanguage = $args[1]
- }
- 3 {
- # 用户同时指定了源语言和目标语言
- $sourceLanguage = $args[1]
- $targetLanguage = $args[2]
- }
- }
- $params = @{
- 'Action' = 'TextTranslate'
- 'Region' = $region
- 'ProjectId' = $projectId
- 'SecretId' = $secretId
- 'Source' = $sourceLanguage
- 'Target' = $targetLanguage
- 'SourceText' = $sourceText
- 'Timestamp' = [System.Convert]::ToString((Get-Date).ToUniversalTime().Subtract((Get-Date "1970-01-01")).TotalSeconds)
- 'Nonce' = [System.Convert]::ToString((Get-Random).Next(1000000))
- 'Version' = '2018-03-21'
- 'Signature' = ''
- }
- $params['Signature'] = $this.getReqSign($params, $secretKey)
- $url = 'https://tmt.tencentcloudapi.com/'
- $response = $this.doHttpPost($url, $params)
- $responseArray = $response | ConvertFrom-Json
- if ($responseArray.Response.TargetText)
- {
- return $responseArray.Response.TargetText
- }
- elseif ($responseArray.Response.Error)
- {
- $errorCode = $responseArray.Response.Error.Code
- $errorMessage = $responseArray.Response.Error.Message
- return "获取翻译失败。错误代码:$errorCode,错误描述:$errorMessage"
- }
- else
- {
- return "获取翻译失败。未知错误。"
- }
- }
- # 一次翻译多个句子
- function translateStrings($sourceTextList)
- {
- $secretId = $this.secretId
- $secretKey = $this.secretKey
- $projectId = $this.projectId
- $region = $this.region
- $sourceLanguage = ""
- $targetLanguage = ""
- # 取得传入参数的数组
- $count = $args.Length
- switch ($count)
- {
- 1 {
- $sourceLanguage = $this.sourceLanguage
- $targetLanguage = $this.targetLanguage
- }
- 2 {
- $sourceLanguage = $this.sourceLanguage
- $targetLanguage = $args[1]
- }
- 3 {
- $sourceLanguage = $args[1]
- $targetLanguage = $args[2]
- }
- }
- $params = @{
- 'Action' = 'TextTranslateBatch'
- 'Region' = $region
- 'ProjectId' = $projectId
- 'SecretId' = $secretId
- 'Source' = $sourceLanguage
- 'Target' = $targetLanguage
- 'Timestamp' = [System.Convert]::ToString((Get-Date).ToUniversalTime().Subtract((Get-Date "1970-01-01")).TotalSeconds)
- 'Nonce' = [System.Convert]::ToString((Get-Random).Next(1000000))
- 'Version' = '2018-03-21'
- 'Signature' = ''
- }
- $counter = 0
- foreach ($sourceText in $sourceTextList)
- {
- $itemName = "SourceTextList.$counter"
- $params[$itemName] = $sourceText
- $counter += 1
- }
- $params['Signature'] = $this.getReqSign($params, $secretKey)
- $url = 'https://tmt.tencentcloudapi.com/'
- $response = $this.doHttpPost($url, $params)
- $responseArray = $response | ConvertFrom-Json
- if ($responseArray.Response.TargetTextList)
- {
- return $responseArray.Response.TargetTextList
- }
- elseif ($responseArray.Response.Error)
- {
- $errorCode = $responseArray.Response.Error.Code
- $errorMessage = $responseArray.Response.Error.Message
- return "获取翻译失败。错误代码:$errorCode,错误描述:$errorMessage"
- }
- else
- {
- return "获取翻译失败。未知错误。"
- }
- }
- # =========================================================================
- # 私有成员函数
- # =========================================================================
- # getReqSign :根据接口请求参数和应用密钥计算请求签名
- function getReqSign($params, $appkey)
- {
- # 对参数排序
- $params = $params.GetEnumerator() | Sort-Object Name
- $signStr = ""
- foreach ($param in $params)
- {
- $signStr += $param.Key + "=" + $param.Value
- }
- # 拼接签名原文字符串
- $signStr = "POSTtmt.tencentcloudapi.com/?" + $signStr
- # 生成签名串
- $hmacsha1 = New-Object System.Security.Cryptography.HMACSHA1
- $byteArray = [System.Text.Encoding]::UTF8.GetBytes($appkey)
- $hmacsha1.Key = $byteArray
- $signBytes = $hmacsha1.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($signStr))
- $sign = [System.Convert]::ToBase64String($signBytes)
- return $sign
- }
- # doHttpPost :执行POST请求,并取回响应结果
- function doHttpPost($url, $params)
- {
- $client = New-Object System.Net.WebClient
- $body = [System.Text.Encoding]::UTF8.GetBytes(($params | ConvertTo-Json))
- $client.Headers["Content-Type"] = "application/json"
- $response = $client.UploadData($url, "POST", $body)
- return [System.Text.Encoding]::UTF8.GetString($response)
- }
- # -------------------------------------------------------------------------
- }
- $translator = [QQTranslator]::new()
- $translator.setSecretId("")
- $translator.setSecretKey("")
- $translator.setProjectId(0)
- $translator.setSourceLanguage("auto")
- $translator.setTargetLanguage("zh")
- $sourceText = "你好"
- $translation = $translator.translateString($sourceText)
- Write-Host "翻译结果: $translation"
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |