Skip to content

Commit

Permalink
fix: unable to translate
Browse files Browse the repository at this point in the history
  • Loading branch information
missuo committed Nov 1, 2024
1 parent c678e87 commit 62a993b
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 388 deletions.
128 changes: 64 additions & 64 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* @Author: Vincent Yang
* @Date: 2023-07-01 21:45:34
* @LastEditors: Vincent Young
* @LastEditTime: 2024-09-16 12:12:35
* @LastEditors: Vincent Yang
* @LastEditTime: 2024-11-01 00:42:58
* @FilePath: /DeepLX/main.go
* @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo
Expand Down Expand Up @@ -156,68 +156,68 @@ func main() {
})

// Pro API endpoint, Pro Account required
r.POST("/v1/translate", authMiddleware(cfg), func(c *gin.Context) {
req := PayloadFree{}
c.BindJSON(&req)

sourceLang := req.SourceLang
targetLang := req.TargetLang
translateText := req.TransText
tagHandling := req.TagHandling
proxyURL := cfg.Proxy

dlSession := cfg.DlSession

if tagHandling != "" && tagHandling != "html" && tagHandling != "xml" {
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": "Invalid tag_handling value. Allowed values are 'html' and 'xml'.",
})
return
}

cookie := c.GetHeader("Cookie")
if cookie != "" {
dlSession = strings.Replace(cookie, "dl_session=", "", -1)
}

if dlSession == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"message": "No dl_session Found",
})
return
} else if strings.Contains(dlSession, ".") {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"message": "Your account is not a Pro account. Please upgrade your account or switch to a different account.",
})
return
}

result, err := translate.TranslateByDeepLXPro(sourceLang, targetLang, translateText, tagHandling, dlSession, proxyURL)
if err != nil {
log.Fatalf("Translation failed: %s", err)
}

if result.Code == http.StatusOK {
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"id": result.ID,
"data": result.Data,
"alternatives": result.Alternatives,
"source_lang": result.SourceLang,
"target_lang": result.TargetLang,
"method": result.Method,
})
} else {
c.JSON(result.Code, gin.H{
"code": result.Code,
"message": result.Message,
})

}
})
// r.POST("/v1/translate", authMiddleware(cfg), func(c *gin.Context) {
// req := PayloadFree{}
// c.BindJSON(&req)

// sourceLang := req.SourceLang
// targetLang := req.TargetLang
// translateText := req.TransText
// tagHandling := req.TagHandling
// proxyURL := cfg.Proxy

// dlSession := cfg.DlSession

// if tagHandling != "" && tagHandling != "html" && tagHandling != "xml" {
// c.JSON(http.StatusBadRequest, gin.H{
// "code": http.StatusBadRequest,
// "message": "Invalid tag_handling value. Allowed values are 'html' and 'xml'.",
// })
// return
// }

// cookie := c.GetHeader("Cookie")
// if cookie != "" {
// dlSession = strings.Replace(cookie, "dl_session=", "", -1)
// }

// if dlSession == "" {
// c.JSON(http.StatusUnauthorized, gin.H{
// "code": http.StatusUnauthorized,
// "message": "No dl_session Found",
// })
// return
// } else if strings.Contains(dlSession, ".") {
// c.JSON(http.StatusUnauthorized, gin.H{
// "code": http.StatusUnauthorized,
// "message": "Your account is not a Pro account. Please upgrade your account or switch to a different account.",
// })
// return
// }

// result, err := translate.TranslateByDeepLXPro(sourceLang, targetLang, translateText, tagHandling, dlSession, proxyURL)
// if err != nil {
// log.Fatalf("Translation failed: %s", err)
// }

// if result.Code == http.StatusOK {
// c.JSON(http.StatusOK, gin.H{
// "code": http.StatusOK,
// "id": result.ID,
// "data": result.Data,
// "alternatives": result.Alternatives,
// "source_lang": result.SourceLang,
// "target_lang": result.TargetLang,
// "method": result.Method,
// })
// } else {
// c.JSON(result.Code, gin.H{
// "code": result.Code,
// "message": result.Message,
// })

// }
// })

// Free API endpoint, Consistent with the official API format
r.POST("/v2/translate", authMiddleware(cfg), func(c *gin.Context) {
Expand Down
Loading

0 comments on commit 62a993b

Please sign in to comment.