12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package controller
- import (
- "encoding/json"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "hongze/hongze_google_api/controller/resp"
- "hongze/hongze_google_api/global"
- "hongze/hongze_google_api/models"
- "hongze/hongze_google_api/services"
- )
- type GoogleTranslateController struct{}
- func (er *GoogleTranslateController) Translate(c *gin.Context) {
- var req models.GoogleTranslateReq
- err := c.ShouldBind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- contentMap := make(map[string]string)
- err = json.Unmarshal([]byte(req.Content), &contentMap)
- if err != nil {
- resp.FailMsg("待翻译的内容解析成json失败", "失败,Err:"+err.Error(), c)
- return
- }
- contentEnMap, err := services.BatchTranslateHandlerByGoogle(contentMap)
- if err != nil {
- resp.FailMsg("翻译失败", "翻译失败,Err:"+err.Error(), c)
- return
- }
- bytes, err := json.Marshal(contentEnMap)
- if err != nil {
- resp.FailMsg("翻译失败", "翻译失败,json.Marshal Err:"+err.Error(), c)
- return
- }
- contentEnStr := string(bytes)
- baseData := new(models.GoogleTranslateResp)
- baseData.ContentEn = contentEnStr
- resp.OkData("操作成功", baseData, c)
- }
- func (er *GoogleTranslateController) SingleTranslate(c *gin.Context) {
- var req models.GoogleTranslateReq
- err := c.ShouldBind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- if req.Content == "" {
- resp.FailMsg("请输入要翻译的内容", "请输入要翻译的内容", c)
- return
- }
- contentEnStr, err := services.SingleTranslateHandlerByGoogle(req.Content)
- if err != nil {
- resp.FailMsg("翻译失败", "翻译失败,Err:"+err.Error(), c)
- return
- }
- baseData := new(models.GoogleTranslateResp)
- baseData.ContentEn = contentEnStr
- resp.OkData("操作成功", baseData, c)
- }
|