Skip to content

Commit

Permalink
feat: support tag_handling
Browse files Browse the repository at this point in the history
  • Loading branch information
missuo committed Sep 15, 2024
1 parent 0d73552 commit e9b5ded
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
27 changes: 23 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Vincent Yang
* @Date: 2023-07-01 21:45:34
* @LastEditors: Vincent Yang
* @LastEditTime: 2024-06-18 02:41:55
* @LastEditTime: 2024-09-15 02:00:23
* @FilePath: /DeepLX/main.go
* @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo
Expand Down Expand Up @@ -106,10 +106,20 @@ func main() {
sourceLang := req.SourceLang
targetLang := req.TargetLang
translateText := req.TransText
tagHandling := req.TagHandling

authKey := cfg.AuthKey
proxyURL := cfg.Proxy

result, err := translateByDeepLX(sourceLang, targetLang, translateText, authKey, proxyURL)
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
}

result, err := translateByDeepLX(sourceLang, targetLang, translateText, tagHandling, authKey, proxyURL)
if err != nil {
log.Fatalf("Translation failed: %s", err)
}
Expand Down Expand Up @@ -141,10 +151,19 @@ func main() {
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)
Expand All @@ -164,7 +183,7 @@ func main() {
return
}

result, err := translateByDeepLXPro(sourceLang, targetLang, translateText, dlSession, proxyURL)
result, err := translateByDeepLXPro(sourceLang, targetLang, translateText, tagHandling, dlSession, proxyURL)
if err != nil {
log.Fatalf("Translation failed: %s", err)
}
Expand Down Expand Up @@ -225,7 +244,7 @@ func main() {
targetLang = jsonData.TargetLang
}

result, err := translateByDeepLX("", targetLang, translateText, authKey, proxyURL)
result, err := translateByDeepLX("", targetLang, translateText, "", authKey, proxyURL)
if err != nil {
log.Fatalf("Translation failed: %s", err)
}
Expand Down
12 changes: 10 additions & 2 deletions translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func translateByOfficialAPI(text string, sourceLang string, targetLang string, a
return sb.String(), nil
}

func translateByDeepLX(sourceLang string, targetLang string, translateText string, authKey string, proxyURL string) (DeepLXTranslationResult, error) {
func translateByDeepLX(sourceLang string, targetLang string, translateText string, tagHandling string, authKey string, proxyURL string) (DeepLXTranslationResult, error) {
id := getRandomNumber()
if sourceLang == "" {
lang := whatlanggo.DetectLang(translateText)
Expand Down Expand Up @@ -142,6 +142,10 @@ func translateByDeepLX(sourceLang string, targetLang string, translateText strin
postData.Params.Texts = append(postData.Params.Texts, text)
postData.Params.Timestamp = getTimeStamp(getICount(translateText))

if tagHandling == "html" || tagHandling == "xml" {
postData.Params.TagHandling = tagHandling
}

// Marshalling the request data to JSON and making necessary string replacements
post_byte, _ := json.Marshal(postData)
postStr := string(post_byte)
Expand Down Expand Up @@ -288,7 +292,7 @@ func translateByDeepLX(sourceLang string, targetLang string, translateText strin
}, nil
}

func translateByDeepLXPro(sourceLang string, targetLang string, translateText string, dlSession string, proxyURL string) (DeepLXTranslationResult, error) {
func translateByDeepLXPro(sourceLang string, targetLang string, translateText string, tagHandling string, dlSession string, proxyURL string) (DeepLXTranslationResult, error) {
id := getRandomNumber()
if sourceLang == "" {
lang := whatlanggo.DetectLang(translateText)
Expand Down Expand Up @@ -319,6 +323,10 @@ func translateByDeepLXPro(sourceLang string, targetLang string, translateText st
postData.Params.Texts = append(postData.Params.Texts, text)
postData.Params.Timestamp = getTimeStamp(getICount(translateText))

if tagHandling == "html" || tagHandling == "xml" {
postData.Params.TagHandling = tagHandling
}

// Marshalling the request data to JSON and making necessary string replacements
post_byte, _ := json.Marshal(postData)
postStr := string(post_byte)
Expand Down
17 changes: 10 additions & 7 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Vincent Yang
* @Date: 2024-03-20 15:43:57
* @LastEditors: Vincent Yang
* @LastEditTime: 2024-06-18 02:38:48
* @LastEditTime: 2024-09-15 01:54:05
* @FilePath: /DeepLX/types.go
* @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo
Expand Down Expand Up @@ -37,6 +37,7 @@ type Params struct {
Lang Lang `json:"lang"`
Timestamp int64 `json:"timestamp"`
CommonJobParams CommonJobParams `json:"commonJobParams"`
TagHandling string `json:"tag_handling"`
}

type Text struct {
Expand All @@ -52,15 +53,17 @@ type PostData struct {
}

type PayloadFree struct {
TransText string `json:"text"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
TransText string `json:"text"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
TagHandling string `json:"tag_handling"`
}

type PayloadAPI struct {
Text []string `json:"text"`
TargetLang string `json:"target_lang"`
SourceLang string `json:"source_lang"`
Text []string `json:"text"`
TargetLang string `json:"target_lang"`
SourceLang string `json:"source_lang"`
TagHandling string `json:"tag_handling"`
}

type Translation struct {
Expand Down

0 comments on commit e9b5ded

Please sign in to comment.