mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2025-06-08 18:03:25 +00:00
fix: handle line breaks #151
This commit is contained in:
parent
4a77cbf30e
commit
bbfb808793
@ -2,7 +2,7 @@
|
||||
* @Author: Vincent Young
|
||||
* @Date: 2024-09-16 11:59:24
|
||||
* @LastEditors: Vincent Yang
|
||||
* @LastEditTime: 2024-12-03 11:23:23
|
||||
* @LastEditTime: 2025-01-20 17:09:59
|
||||
* @FilePath: /DeepLX/translate/translate.go
|
||||
* @Telegram: https://t.me/missuo
|
||||
* @GitHub: https://github.com/missuo
|
||||
@ -126,8 +126,20 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Split text by newlines and store them for later reconstruction
|
||||
textParts := strings.Split(text, "\n")
|
||||
var translatedParts []string
|
||||
var allAlternatives [][]string // Store alternatives for each part
|
||||
|
||||
for _, part := range textParts {
|
||||
if strings.TrimSpace(part) == "" {
|
||||
translatedParts = append(translatedParts, "")
|
||||
allAlternatives = append(allAlternatives, []string{""})
|
||||
continue
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return DeepLXTranslationResult{
|
||||
Code: http.StatusServiceUnavailable,
|
||||
@ -137,7 +149,7 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
||||
|
||||
// Get detected language if source language is auto
|
||||
if sourceLang == "auto" || sourceLang == "" {
|
||||
sourceLang = strings.ToUpper(whatlanggo.DetectLang(text).Iso6391())
|
||||
sourceLang = strings.ToUpper(whatlanggo.DetectLang(part).Iso6391())
|
||||
}
|
||||
|
||||
// Prepare jobs from split result
|
||||
@ -194,7 +206,7 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
||||
},
|
||||
Jobs: jobs,
|
||||
Priority: 1,
|
||||
Timestamp: getTimeStamp(getICount(text)),
|
||||
Timestamp: getTimeStamp(getICount(part)),
|
||||
},
|
||||
}
|
||||
|
||||
@ -214,7 +226,7 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
||||
},
|
||||
Jobs: jobs,
|
||||
Priority: 1,
|
||||
Timestamp: getTimeStamp(getICount(text)),
|
||||
Timestamp: getTimeStamp(getICount(part)),
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -229,45 +241,76 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
||||
}
|
||||
|
||||
// Process translation results
|
||||
var alternatives []string
|
||||
var translatedText string
|
||||
var partTranslation string
|
||||
var partAlternatives []string
|
||||
|
||||
translations := result.Get("result.translations").Array()
|
||||
if len(translations) > 0 {
|
||||
// Get alternatives
|
||||
// Process main translation
|
||||
for _, translation := range translations {
|
||||
partTranslation += translation.Get("beams.0.sentences.0.text").String() + " "
|
||||
}
|
||||
partTranslation = strings.TrimSpace(partTranslation)
|
||||
|
||||
// Process alternatives
|
||||
numBeams := len(translations[0].Get("beams").Array())
|
||||
for i := 0; i < numBeams; i++ {
|
||||
for i := 1; i < numBeams; i++ { // Start from 1 since 0 is the main translation
|
||||
var altText string
|
||||
for _, translation := range translations {
|
||||
beams := translation.Get("beams").Array()
|
||||
if i < len(beams) {
|
||||
altText += beams[i].Get("sentences.0.text").String()
|
||||
altText += beams[i].Get("sentences.0.text").String() + " "
|
||||
}
|
||||
}
|
||||
if altText != "" {
|
||||
alternatives = append(alternatives, altText)
|
||||
partAlternatives = append(partAlternatives, strings.TrimSpace(altText))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get main translation
|
||||
for _, translation := range translations {
|
||||
translatedText += translation.Get("beams.0.sentences.0.text").String() + " "
|
||||
}
|
||||
translatedText = strings.TrimSpace(translatedText)
|
||||
}
|
||||
|
||||
if translatedText == "" {
|
||||
if partTranslation == "" {
|
||||
return DeepLXTranslationResult{
|
||||
Code: http.StatusServiceUnavailable,
|
||||
Message: "Translation failed",
|
||||
}, nil
|
||||
}
|
||||
|
||||
translatedParts = append(translatedParts, partTranslation)
|
||||
allAlternatives = append(allAlternatives, partAlternatives)
|
||||
}
|
||||
|
||||
// Join all translated parts with newlines
|
||||
translatedText := strings.Join(translatedParts, "\n")
|
||||
|
||||
// Combine alternatives with proper newline handling
|
||||
var combinedAlternatives []string
|
||||
maxAlts := 0
|
||||
for _, alts := range allAlternatives {
|
||||
if len(alts) > maxAlts {
|
||||
maxAlts = len(alts)
|
||||
}
|
||||
}
|
||||
|
||||
// Create combined alternatives preserving line structure
|
||||
for i := 0; i < maxAlts; i++ {
|
||||
var altParts []string
|
||||
for j, alts := range allAlternatives {
|
||||
if i < len(alts) {
|
||||
altParts = append(altParts, alts[i])
|
||||
} else if len(translatedParts[j]) == 0 {
|
||||
altParts = append(altParts, "") // Keep empty lines
|
||||
} else {
|
||||
altParts = append(altParts, translatedParts[j]) // Use main translation if no alternative
|
||||
}
|
||||
}
|
||||
combinedAlternatives = append(combinedAlternatives, strings.Join(altParts, "\n"))
|
||||
}
|
||||
|
||||
return DeepLXTranslationResult{
|
||||
Code: http.StatusOK,
|
||||
ID: id,
|
||||
ID: getRandomNumber(), // Using new ID for the complete translation
|
||||
Data: translatedText,
|
||||
Alternatives: alternatives,
|
||||
Alternatives: combinedAlternatives,
|
||||
SourceLang: sourceLang,
|
||||
TargetLang: targetLang,
|
||||
Method: map[bool]string{true: "Pro", false: "Free"}[dlSession != ""],
|
||||
|
Loading…
Reference in New Issue
Block a user