Skip to content

Commit

Permalink
fix: targetlang bug
Browse files Browse the repository at this point in the history
  • Loading branch information
missuo committed Nov 2, 2024
1 parent d98f71d commit ede6229
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
44 changes: 26 additions & 18 deletions translate/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Vincent Young
* @Date: 2024-09-16 11:59:24
* @LastEditors: Vincent Yang
* @LastEditTime: 2024-11-01 13:12:25
* @LastEditTime: 2024-11-01 23:19:11
* @FilePath: /DeepLX/translate/translate.go
* @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo
Expand Down Expand Up @@ -93,20 +93,6 @@ func makeRequest(postData *PostData, urlMethod string, proxyURL string, dlSessio
}
defer resp.Body.Close()

// Check status code before processing response
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusTooManyRequests:
return gjson.Result{}, fmt.Errorf("too many requests")
case http.StatusUnauthorized:
return gjson.Result{}, fmt.Errorf("unauthorized")
case http.StatusForbidden:
return gjson.Result{}, fmt.Errorf("forbidden")
default:
return gjson.Result{}, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
}

var bodyReader io.Reader
if resp.Header.Get("Content-Encoding") == "br" {
bodyReader = brotli.NewReader(resp.Body)
Expand Down Expand Up @@ -195,22 +181,23 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
}

hasRegionalVariant := false
targetLangCode := targetLang
targetLangParts := strings.Split(targetLang, "-")
targetLangCode := targetLangParts[0]
if len(targetLangParts) > 1 {
targetLangCode = targetLangParts[0]
hasRegionalVariant = true
}

// Prepare translation request
id := getRandomNumber()

postData := &PostData{
Jsonrpc: "2.0",
Method: "LMT_handle_jobs",
ID: id,
Params: Params{
CommonJobParams: CommonJobParams{
Mode: "translate",
RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant],
Mode: "translate",
},
Lang: Lang{
SourceLangComputed: strings.ToUpper(sourceLang),
Expand All @@ -222,6 +209,27 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
},
}

if hasRegionalVariant {
postData = &PostData{
Jsonrpc: "2.0",
Method: "LMT_handle_jobs",
ID: id,
Params: Params{
CommonJobParams: CommonJobParams{
Mode: "translate",
RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant],
},
Lang: Lang{
SourceLangComputed: strings.ToUpper(sourceLang),
TargetLang: strings.ToUpper(targetLangCode),
},
Jobs: jobs,
Priority: 1,
Timestamp: getTimeStamp(getICount(text)),
},
}
}

// Make translation request
result, err := makeRequest(postData, "LMT_handle_jobs", proxyURL, dlSession)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions translate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Vincent Young
* @Date: 2024-09-16 11:59:24
* @LastEditors: Vincent Yang
* @LastEditTime: 2024-11-01 12:47:47
* @LastEditTime: 2024-11-01 23:18:56
* @FilePath: /DeepLX/translate/types.go
* @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo
Expand All @@ -22,7 +22,7 @@ type Lang struct {
// CommonJobParams represents common parameters for translation jobs
type CommonJobParams struct {
Mode string `json:"mode"`
RegionalVariant string `json:"regionalVariant"`
RegionalVariant string `json:"regionalVariant,omitempty"`
}

// Sentence represents a sentence in the translation request
Expand Down

0 comments on commit ede6229

Please sign in to comment.