aliyun_translate.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. alimt20181012 "github.com/alibabacloud-go/alimt-20181012/v2/client"
  6. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  7. util "github.com/alibabacloud-go/tea-utils/v2/service"
  8. "github.com/alibabacloud-go/tea/tea"
  9. )
  10. /**
  11. * 使用STS鉴权方式初始化账号Client,推荐此方式。本示例默认使用AK&SK方式。
  12. * @param accessKeyId
  13. * @param accessKeySecret
  14. * @param securityToken
  15. * @return Client
  16. * @throws Exception
  17. */
  18. func CreateClientWithSTS (accessKeyId *string, accessKeySecret *string, securityToken *string) (_result *alimt20181012.Client, _err error) {
  19. config := &openapi.Config{
  20. // 必填,您的 AccessKey ID
  21. AccessKeyId: accessKeyId,
  22. // 必填,您的 AccessKey Secret
  23. AccessKeySecret: accessKeySecret,
  24. // 必填,您的 Security Token
  25. SecurityToken: securityToken,
  26. // 必填,表明使用 STS 方式
  27. Type: tea.String("sts"),
  28. }
  29. // 访问的域名
  30. config.Endpoint = tea.String("mt.cn-hangzhou.aliyuncs.com")
  31. _result = &alimt20181012.Client{}
  32. _result, _err = alimt20181012.NewClient(config)
  33. return _result, _err
  34. }
  35. func handler (accessKeyId, accessKeySecret, stsToken *string, content string) (resp *alimt20181012.TranslateGeneralResponseBody, _err error) {
  36. // 初始化 Client,采用 AK&SK 鉴权访问的方式,此方式可能会存在泄漏风险,建议使用 STS 方式。鉴权访问方式请参考:https://help.aliyun.com/document_detail/378661.html
  37. //client, _err := CreateClient(accessKeyId, accessKeySecret)
  38. client, _err := CreateClientWithSTS(accessKeyId, accessKeySecret, stsToken)
  39. if _err != nil {
  40. _err = errors.New(fmt.Sprintf("创建翻译客户端失败 err: %v", _err))
  41. return
  42. }
  43. translateGeneralRequest := &alimt20181012.TranslateGeneralRequest{
  44. FormatType: tea.String("text"),
  45. SourceLanguage: tea.String("zh"),
  46. TargetLanguage: tea.String("en"),
  47. SourceText: tea.String(content),
  48. Scene: tea.String("general"),
  49. }
  50. runtime := &util.RuntimeOptions{}
  51. ret, tryErr := func()(ret *alimt20181012.TranslateGeneralResponse, _e error) {
  52. defer func() {
  53. if r := tea.Recover(recover()); r != nil {
  54. _e = r
  55. return
  56. }
  57. }()
  58. // 复制代码运行请自行打印 API 的返回值
  59. ret, _e = client.TranslateGeneralWithOptions(translateGeneralRequest, runtime)
  60. if _e != nil {
  61. return
  62. }
  63. // fmt.Println(ret)
  64. return
  65. }()
  66. if tryErr != nil {
  67. var e = &tea.SDKError{}
  68. if _t, ok := tryErr.(*tea.SDKError); ok {
  69. e = _t
  70. } else {
  71. e.Message = tea.String(tryErr.Error())
  72. }
  73. // 如有需要,请打印 e
  74. _, _err = util.AssertAsString(e.Message)
  75. if _err != nil {
  76. _err = errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
  77. return
  78. }
  79. _err = errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
  80. return
  81. }
  82. if *ret.StatusCode != 200 {
  83. _err = errors.New(fmt.Sprintf(" %v", ret.StatusCode))
  84. return
  85. }
  86. resp = ret.Body
  87. return
  88. }
  89. func batchHandler (accessKeyId, accessKeySecret, stsToken *string, content string) (resp *alimt20181012.GetBatchTranslateResponseBody, _err error) {
  90. // 初始化 Client,采用 AK&SK 鉴权访问的方式,此方式可能会存在泄漏风险,建议使用 STS 方式。鉴权访问方式请参考:https://help.aliyun.com/document_detail/378661.html
  91. //client, _err := CreateClient(accessKeyId, accessKeySecret)
  92. client, _err := CreateClientWithSTS(accessKeyId, accessKeySecret, stsToken)
  93. if _err != nil {
  94. _err = errors.New(fmt.Sprintf("创建翻译客户端失败 err: %v", _err))
  95. return
  96. }
  97. getBatchTranslateRequest := &alimt20181012.GetBatchTranslateRequest{
  98. FormatType: tea.String("text"),
  99. TargetLanguage: tea.String("en"),
  100. SourceLanguage: tea.String("zh"),
  101. Scene: tea.String("general"),
  102. ApiType: tea.String("translate_standard"),
  103. SourceText: tea.String(content),
  104. }
  105. runtime := &util.RuntimeOptions{}
  106. ret, tryErr := func()(ret *alimt20181012.GetBatchTranslateResponse, _e error) {
  107. defer func() {
  108. if r := tea.Recover(recover()); r != nil {
  109. _e = r
  110. return
  111. }
  112. }()
  113. // 复制代码运行请自行打印 API 的返回值
  114. ret, _e = client.GetBatchTranslateWithOptions(getBatchTranslateRequest, runtime)
  115. if _e != nil {
  116. return
  117. }
  118. fmt.Println(ret)
  119. return
  120. }()
  121. if tryErr != nil {
  122. var e = &tea.SDKError{}
  123. if _t, ok := tryErr.(*tea.SDKError); ok {
  124. e = _t
  125. } else {
  126. e.Message = tea.String(tryErr.Error())
  127. }
  128. // 如有需要,请打印 e
  129. _, _err = util.AssertAsString(e.Message)
  130. if _err != nil {
  131. _err = errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
  132. return
  133. }
  134. _err = errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
  135. return
  136. }
  137. if *ret.StatusCode != 200 {
  138. _err = errors.New(fmt.Sprintf(" %v", ret.StatusCode))
  139. return
  140. }
  141. resp = ret.Body
  142. return
  143. }
  144. func AliTranslate(content string) (contentEn string, err error) {
  145. stsToken, err := GetOssSTSToken()
  146. if err != nil {
  147. err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
  148. return
  149. }
  150. fmt.Println(stsToken)
  151. resp, err := handler(&stsToken.AccessKeyId, &stsToken.AccessKeySecret, &stsToken.SecurityToken, content)
  152. if err != nil {
  153. err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
  154. return
  155. }
  156. if *resp.Code != 200 {
  157. err = errors.New(fmt.Sprintf("阿里云机器翻译失败 code: %v; msg: %v",*resp.Code, *resp.Message))
  158. return
  159. }
  160. contentEn = *resp.Data.Translated
  161. return
  162. }
  163. type TranslatedListItem struct {
  164. Code string `json:"code"`
  165. WordCount string `json:"wordCount"`
  166. Index string `json:"index"`
  167. Translated string `json:"translated"`
  168. }
  169. func AliTranslateBatch(content string) (contentEnList []map[string]interface{}, err error) {
  170. contentEnList = make([]map[string]interface{}, 0)
  171. stsToken, err := GetOssSTSToken()
  172. if err != nil {
  173. err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
  174. return
  175. }
  176. fmt.Println(stsToken)
  177. resp, err := batchHandler(&stsToken.AccessKeyId, &stsToken.AccessKeySecret, &stsToken.SecurityToken, content)
  178. if err != nil {
  179. err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
  180. return
  181. }
  182. if *resp.Code != 200 {
  183. err = errors.New(fmt.Sprintf("阿里云机器翻译失败 code: %v; msg: %v",*resp.Code, *resp.Message))
  184. return
  185. }
  186. contentEnList = resp.TranslatedList
  187. return
  188. }