|
@@ -64,6 +64,7 @@ func handler (accessKeyId, accessKeySecret, stsToken *string, content string) (r
|
|
|
if _e != nil {
|
|
|
return
|
|
|
}
|
|
|
+ // fmt.Println(ret)
|
|
|
return
|
|
|
}()
|
|
|
|
|
@@ -92,6 +93,64 @@ func handler (accessKeyId, accessKeySecret, stsToken *string, content string) (r
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func batchHandler (accessKeyId, accessKeySecret, stsToken *string, content string) (resp *alimt20181012.GetBatchTranslateResponseBody, _err error) {
|
|
|
+ // 初始化 Client,采用 AK&SK 鉴权访问的方式,此方式可能会存在泄漏风险,建议使用 STS 方式。鉴权访问方式请参考:https://help.aliyun.com/document_detail/378661.html
|
|
|
+ //client, _err := CreateClient(accessKeyId, accessKeySecret)
|
|
|
+ client, _err := CreateClientWithSTS(accessKeyId, accessKeySecret, stsToken)
|
|
|
+ if _err != nil {
|
|
|
+ _err = errors.New(fmt.Sprintf("创建翻译客户端失败 err: %v", _err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ getBatchTranslateRequest := &alimt20181012.GetBatchTranslateRequest{
|
|
|
+ FormatType: tea.String("text"),
|
|
|
+ TargetLanguage: tea.String("en"),
|
|
|
+ SourceLanguage: tea.String("zh"),
|
|
|
+ Scene: tea.String("general"),
|
|
|
+ ApiType: tea.String("translate_standard"),
|
|
|
+ SourceText: tea.String(content),
|
|
|
+ }
|
|
|
+ runtime := &util.RuntimeOptions{}
|
|
|
+ ret, tryErr := func()(ret *alimt20181012.GetBatchTranslateResponse, _e error) {
|
|
|
+ defer func() {
|
|
|
+ if r := tea.Recover(recover()); r != nil {
|
|
|
+ _e = r
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ // 复制代码运行请自行打印 API 的返回值
|
|
|
+ ret, _e = client.GetBatchTranslateWithOptions(getBatchTranslateRequest, runtime)
|
|
|
+ if _e != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println(ret)
|
|
|
+ return
|
|
|
+ }()
|
|
|
+
|
|
|
+
|
|
|
+ if tryErr != nil {
|
|
|
+ var e = &tea.SDKError{}
|
|
|
+ if _t, ok := tryErr.(*tea.SDKError); ok {
|
|
|
+ e = _t
|
|
|
+ } else {
|
|
|
+ e.Message = tea.String(tryErr.Error())
|
|
|
+ }
|
|
|
+ // 如有需要,请打印 e
|
|
|
+ _, _err = util.AssertAsString(e.Message)
|
|
|
+ if _err != nil {
|
|
|
+ _err = errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ _err = errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if *ret.StatusCode != 200 {
|
|
|
+ _err = errors.New(fmt.Sprintf(" %v", ret.StatusCode))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp = ret.Body
|
|
|
+ return
|
|
|
+}
|
|
|
|
|
|
func AliTranslate(content string) (contentEn string, err error) {
|
|
|
stsToken, err := GetOssSTSToken()
|
|
@@ -112,4 +171,33 @@ func AliTranslate(content string) (contentEn string, err error) {
|
|
|
|
|
|
contentEn = *resp.Data.Translated
|
|
|
return
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+type TranslatedListItem struct {
|
|
|
+ Code string `json:"code"`
|
|
|
+ WordCount string `json:"wordCount"`
|
|
|
+ Index string `json:"index"`
|
|
|
+ Translated string `json:"translated"`
|
|
|
+}
|
|
|
+
|
|
|
+func AliTranslateBatch(content string) (contentEnList []map[string]interface{}, err error) {
|
|
|
+ contentEnList = make([]map[string]interface{}, 0)
|
|
|
+ stsToken, err := GetOssSTSToken()
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println(stsToken)
|
|
|
+ resp, err := batchHandler(&stsToken.AccessKeyId, &stsToken.AccessKeySecret, &stsToken.SecurityToken, content)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if *resp.Code != 200 {
|
|
|
+ err = errors.New(fmt.Sprintf("阿里云机器翻译失败 code: %v; msg: %v",*resp.Code, *resp.Message))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ contentEnList = resp.TranslatedList
|
|
|
+ return
|
|
|
+}
|