fix: targetlang bug

This commit is contained in:
Vincent Yang 2024-11-01 23:19:50 -04:00
parent d98f71d1c5
commit ede6229b0e
No known key found for this signature in database
GPG Key ID: 55F1635E821BF0E8
2 changed files with 28 additions and 20 deletions

View File

@ -2,7 +2,7 @@
* @Author: Vincent Young * @Author: Vincent Young
* @Date: 2024-09-16 11:59:24 * @Date: 2024-09-16 11:59:24
* @LastEditors: Vincent Yang * @LastEditors: Vincent Yang
* @LastEditTime: 2024-11-01 13:12:25 * @LastEditTime: 2024-11-01 23:19:11
* @FilePath: /DeepLX/translate/translate.go * @FilePath: /DeepLX/translate/translate.go
* @Telegram: https://t.me/missuo * @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo * @GitHub: https://github.com/missuo
@ -93,20 +93,6 @@ func makeRequest(postData *PostData, urlMethod string, proxyURL string, dlSessio
} }
defer resp.Body.Close() 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 var bodyReader io.Reader
if resp.Header.Get("Content-Encoding") == "br" { if resp.Header.Get("Content-Encoding") == "br" {
bodyReader = brotli.NewReader(resp.Body) bodyReader = brotli.NewReader(resp.Body)
@ -195,15 +181,36 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
} }
hasRegionalVariant := false hasRegionalVariant := false
targetLangCode := targetLang
targetLangParts := strings.Split(targetLang, "-") targetLangParts := strings.Split(targetLang, "-")
targetLangCode := targetLangParts[0]
if len(targetLangParts) > 1 { if len(targetLangParts) > 1 {
targetLangCode = targetLangParts[0]
hasRegionalVariant = true hasRegionalVariant = true
} }
// Prepare translation request // Prepare translation request
id := getRandomNumber() id := getRandomNumber()
postData := &PostData{ postData := &PostData{
Jsonrpc: "2.0",
Method: "LMT_handle_jobs",
ID: id,
Params: Params{
CommonJobParams: CommonJobParams{
Mode: "translate",
},
Lang: Lang{
SourceLangComputed: strings.ToUpper(sourceLang),
TargetLang: strings.ToUpper(targetLangCode),
},
Jobs: jobs,
Priority: 1,
Timestamp: getTimeStamp(getICount(text)),
},
}
if hasRegionalVariant {
postData = &PostData{
Jsonrpc: "2.0", Jsonrpc: "2.0",
Method: "LMT_handle_jobs", Method: "LMT_handle_jobs",
ID: id, ID: id,
@ -221,6 +228,7 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
Timestamp: getTimeStamp(getICount(text)), Timestamp: getTimeStamp(getICount(text)),
}, },
} }
}
// Make translation request // Make translation request
result, err := makeRequest(postData, "LMT_handle_jobs", proxyURL, dlSession) result, err := makeRequest(postData, "LMT_handle_jobs", proxyURL, dlSession)

View File

@ -2,7 +2,7 @@
* @Author: Vincent Young * @Author: Vincent Young
* @Date: 2024-09-16 11:59:24 * @Date: 2024-09-16 11:59:24
* @LastEditors: Vincent Yang * @LastEditors: Vincent Yang
* @LastEditTime: 2024-11-01 12:47:47 * @LastEditTime: 2024-11-01 23:18:56
* @FilePath: /DeepLX/translate/types.go * @FilePath: /DeepLX/translate/types.go
* @Telegram: https://t.me/missuo * @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo * @GitHub: https://github.com/missuo
@ -22,7 +22,7 @@ type Lang struct {
// CommonJobParams represents common parameters for translation jobs // CommonJobParams represents common parameters for translation jobs
type CommonJobParams struct { type CommonJobParams struct {
Mode string `json:"mode"` Mode string `json:"mode"`
RegionalVariant string `json:"regionalVariant"` RegionalVariant string `json:"regionalVariant,omitempty"`
} }
// Sentence represents a sentence in the translation request // Sentence represents a sentence in the translation request