Skip to content

Commit 26f6782

Browse files
Merge branch 'OwO-Network:main' into main
2 parents 50cb159 + ede6229 commit 26f6782

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

translate/translate.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Vincent Young
33
* @Date: 2024-09-16 11:59:24
44
* @LastEditors: Vincent Yang
5-
* @LastEditTime: 2024-11-01 13:12:25
5+
* @LastEditTime: 2024-11-01 23:19:11
66
* @FilePath: /DeepLX/translate/translate.go
77
* @Telegram: https://t.me/missuo
88
* @GitHub: https://github.com/missuo
@@ -93,20 +93,6 @@ func makeRequest(postData *PostData, urlMethod string, proxyURL string, dlSessio
9393
}
9494
defer resp.Body.Close()
9595

96-
// Check status code before processing response
97-
if resp.StatusCode != http.StatusOK {
98-
switch resp.StatusCode {
99-
case http.StatusTooManyRequests:
100-
return gjson.Result{}, fmt.Errorf("too many requests")
101-
case http.StatusUnauthorized:
102-
return gjson.Result{}, fmt.Errorf("unauthorized")
103-
case http.StatusForbidden:
104-
return gjson.Result{}, fmt.Errorf("forbidden")
105-
default:
106-
return gjson.Result{}, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
107-
}
108-
}
109-
11096
var bodyReader io.Reader
11197
if resp.Header.Get("Content-Encoding") == "br" {
11298
bodyReader = brotli.NewReader(resp.Body)
@@ -195,22 +181,23 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
195181
}
196182

197183
hasRegionalVariant := false
184+
targetLangCode := targetLang
198185
targetLangParts := strings.Split(targetLang, "-")
199-
targetLangCode := targetLangParts[0]
200186
if len(targetLangParts) > 1 {
187+
targetLangCode = targetLangParts[0]
201188
hasRegionalVariant = true
202189
}
203190

204191
// Prepare translation request
205192
id := getRandomNumber()
193+
206194
postData := &PostData{
207195
Jsonrpc: "2.0",
208196
Method: "LMT_handle_jobs",
209197
ID: id,
210198
Params: Params{
211199
CommonJobParams: CommonJobParams{
212-
Mode: "translate",
213-
RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant],
200+
Mode: "translate",
214201
},
215202
Lang: Lang{
216203
SourceLangComputed: strings.ToUpper(sourceLang),
@@ -222,6 +209,27 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
222209
},
223210
}
224211

212+
if hasRegionalVariant {
213+
postData = &PostData{
214+
Jsonrpc: "2.0",
215+
Method: "LMT_handle_jobs",
216+
ID: id,
217+
Params: Params{
218+
CommonJobParams: CommonJobParams{
219+
Mode: "translate",
220+
RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant],
221+
},
222+
Lang: Lang{
223+
SourceLangComputed: strings.ToUpper(sourceLang),
224+
TargetLang: strings.ToUpper(targetLangCode),
225+
},
226+
Jobs: jobs,
227+
Priority: 1,
228+
Timestamp: getTimeStamp(getICount(text)),
229+
},
230+
}
231+
}
232+
225233
// Make translation request
226234
result, err := makeRequest(postData, "LMT_handle_jobs", proxyURL, dlSession)
227235
if err != nil {

translate/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Vincent Young
33
* @Date: 2024-09-16 11:59:24
44
* @LastEditors: Vincent Yang
5-
* @LastEditTime: 2024-11-01 12:47:47
5+
* @LastEditTime: 2024-11-01 23:18:56
66
* @FilePath: /DeepLX/translate/types.go
77
* @Telegram: https://t.me/missuo
88
* @GitHub: https://github.com/missuo
@@ -22,7 +22,7 @@ type Lang struct {
2222
// CommonJobParams represents common parameters for translation jobs
2323
type CommonJobParams struct {
2424
Mode string `json:"mode"`
25-
RegionalVariant string `json:"regionalVariant"`
25+
RegionalVariant string `json:"regionalVariant,omitempty"`
2626
}
2727

2828
// Sentence represents a sentence in the translation request

0 commit comments

Comments
 (0)