Compare commits

..
1 Commits
Author SHA1 Message Date
Vincent Yang 85fd738f3e bump to Go 1.23.6 2025-02-09 20:12:06 -05:00
5 changed files with 128 additions and 146 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ jobs:
- name: Set up Go - name: Set up Go
uses: actions/setup-go@v4 uses: actions/setup-go@v4
with: with:
go-version: '1.23.5' go-version: '1.23.6'
- name: Install golint - name: Install golint
run: go install golang.org/x/lint/golint@latest run: go install golang.org/x/lint/golint@latest
+1 -1
View File
@@ -15,7 +15,7 @@ jobs:
- uses: actions/setup-go@v4 - uses: actions/setup-go@v4
with: with:
go-version: "1.23.5" go-version: "1.23.6"
- run: bash .cross_compile.sh - run: bash .cross_compile.sh
+1 -1
View File
@@ -1,4 +1,4 @@
FROM golang:1.23.5 AS builder FROM golang:1.23.6 AS builder
WORKDIR /go/src/github.com/OwO-Network/DeepLX WORKDIR /go/src/github.com/OwO-Network/DeepLX
COPY . . COPY . .
RUN go get -d -v ./ RUN go get -d -v ./
+1 -1
View File
@@ -1,6 +1,6 @@
module github.com/OwO-Network/DeepLX module github.com/OwO-Network/DeepLX
go 1.23.5 go 1.23.6
require ( require (
github.com/abadojack/whatlanggo v1.0.1 github.com/abadojack/whatlanggo v1.0.1
+51 -69
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: 2025-02-01 03:21:41 * @LastEditTime: 2025-01-20 17:09:59
* @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
@@ -19,7 +19,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"sync"
"github.com/abadojack/whatlanggo" "github.com/abadojack/whatlanggo"
"github.com/imroc/req/v3" "github.com/imroc/req/v3"
@@ -127,47 +126,30 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
}, nil }, nil
} }
// Split text by newlines // Split text by newlines and store them for later reconstruction
textParts := strings.Split(text, "\n") textParts := strings.Split(text, "\n")
var translatedParts []string
var allAlternatives [][]string // Store alternatives for each part
// Create channels for results for _, part := range textParts {
type translationResult struct { if strings.TrimSpace(part) == "" {
index int translatedParts = append(translatedParts, "")
translation string allAlternatives = append(allAlternatives, []string{""})
alternatives []string continue
err error
}
results := make(chan translationResult, len(textParts))
// Create a wait group to track all goroutines
var wg sync.WaitGroup
// Launch goroutines for each text part
for i := range textParts {
wg.Add(1)
go func(index int, text string) {
defer wg.Done()
if strings.TrimSpace(text) == "" {
results <- translationResult{
index: index,
translation: "",
alternatives: []string{""},
}
return
} }
// Split text first // Split text first
splitResult, err := splitText(text, tagHandling == "html" || tagHandling == "xml", proxyURL, dlSession) splitResult, err := splitText(part, tagHandling == "html" || tagHandling == "xml", proxyURL, dlSession)
if err != nil { if err != nil {
results <- translationResult{index: index, err: err} return DeepLXTranslationResult{
return Code: http.StatusServiceUnavailable,
Message: err.Error(),
}, nil
} }
// Get detected language if source language is auto // Get detected language if source language is auto
currentSourceLang := sourceLang if sourceLang == "auto" || sourceLang == "" {
if currentSourceLang == "auto" || currentSourceLang == "" { sourceLang = strings.ToUpper(whatlanggo.DetectLang(part).Iso6391())
currentSourceLang = strings.ToUpper(whatlanggo.DetectLang(text).Iso6391())
} }
// Prepare jobs from split result // Prepare jobs from split result
@@ -209,7 +191,27 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
// 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(part)),
},
}
if hasRegionalVariant {
postData = &PostData{
Jsonrpc: "2.0", Jsonrpc: "2.0",
Method: "LMT_handle_jobs", Method: "LMT_handle_jobs",
ID: id, ID: id,
@@ -219,20 +221,23 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant], RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant],
}, },
Lang: Lang{ Lang: Lang{
SourceLangComputed: strings.ToUpper(currentSourceLang), SourceLangComputed: strings.ToUpper(sourceLang),
TargetLang: strings.ToUpper(targetLangCode), TargetLang: strings.ToUpper(targetLangCode),
}, },
Jobs: jobs, Jobs: jobs,
Priority: 1, Priority: 1,
Timestamp: getTimeStamp(getICount(text)), Timestamp: getTimeStamp(getICount(part)),
}, },
} }
}
// Make translation request // Make translation request
result, err := makeRequest(postData, "LMT_handle_jobs", proxyURL, dlSession) result, err := makeRequest(postData, "LMT_handle_jobs", proxyURL, dlSession)
if err != nil { if err != nil {
results <- translationResult{index: index, err: err} return DeepLXTranslationResult{
return Code: http.StatusServiceUnavailable,
Message: err.Error(),
}, nil
} }
// Process translation results // Process translation results
@@ -249,7 +254,7 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
// Process alternatives // Process alternatives
numBeams := len(translations[0].Get("beams").Array()) numBeams := len(translations[0].Get("beams").Array())
for i := 1; i < numBeams; i++ { for i := 1; i < numBeams; i++ { // Start from 1 since 0 is the main translation
var altText string var altText string
for _, translation := range translations { for _, translation := range translations {
beams := translation.Get("beams").Array() beams := translation.Get("beams").Array()
@@ -264,37 +269,14 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
} }
if partTranslation == "" { if partTranslation == "" {
results <- translationResult{index: index, err: fmt.Errorf("translation failed")}
return
}
results <- translationResult{
index: index,
translation: partTranslation,
alternatives: partAlternatives,
}
}(i, textParts[i])
}
// Close results channel when all goroutines are done
go func() {
wg.Wait()
close(results)
}()
// Collect results maintaining original order
translatedParts := make([]string, len(textParts))
allAlternatives := make([][]string, len(textParts))
for result := range results {
if result.err != nil {
return DeepLXTranslationResult{ return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable, Code: http.StatusServiceUnavailable,
Message: result.err.Error(), Message: "Translation failed",
}, nil }, nil
} }
translatedParts[result.index] = result.translation
allAlternatives[result.index] = result.alternatives translatedParts = append(translatedParts, partTranslation)
allAlternatives = append(allAlternatives, partAlternatives)
} }
// Join all translated parts with newlines // Join all translated parts with newlines
@@ -316,9 +298,9 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
if i < len(alts) { if i < len(alts) {
altParts = append(altParts, alts[i]) altParts = append(altParts, alts[i])
} else if len(translatedParts[j]) == 0 { } else if len(translatedParts[j]) == 0 {
altParts = append(altParts, "") altParts = append(altParts, "") // Keep empty lines
} else { } else {
altParts = append(altParts, translatedParts[j]) altParts = append(altParts, translatedParts[j]) // Use main translation if no alternative
} }
} }
combinedAlternatives = append(combinedAlternatives, strings.Join(altParts, "\n")) combinedAlternatives = append(combinedAlternatives, strings.Join(altParts, "\n"))
@@ -326,7 +308,7 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
return DeepLXTranslationResult{ return DeepLXTranslationResult{
Code: http.StatusOK, Code: http.StatusOK,
ID: getRandomNumber(), ID: getRandomNumber(), // Using new ID for the complete translation
Data: translatedText, Data: translatedText,
Alternatives: combinedAlternatives, Alternatives: combinedAlternatives,
SourceLang: sourceLang, SourceLang: sourceLang,