fix(translate): answer blank texts with themselves instead of failing the request

A request fails as a whole when any one of its texts is empty, so a document
split into lines cannot be translated at all if it has a blank line in it:
["", "Hello"] answers 404 "No text to translate" and nothing is translated.

A blank text — empty, or nothing but whitespace — carries nothing to
translate, so it is no longer sent, and its own text is the answer. That
keeps the response position-aligned, and it is the only thing that can work:
oneshot echoes an empty input, and the positional check would read that echo
as a failed translation and answer 503 instead. A whitespace-only text
survives today only because oneshot happens to echo it as well.

The 404 is now only for a request with no text at all — no texts, or every
text empty. A request that has text but nothing to translate, like ["   "],
is answered with its own texts, so no blank input errors any more and no
input that used to succeed fails now.

This matches the JavaScript client (@deeplx/core batchPayload and
restoreBlankSegments), which already drops text.trim() === '' segments and
puts them back, so the two sides agree on what a blank segment means.

Refs #240, #241, #244.
This commit is contained in:
JounQin
2026-10-10 23:35:19 +08:00
parent 83f97d336d
commit 7269cf615a
4 changed files with 139 additions and 42 deletions
+3 -3
View File
@@ -104,12 +104,12 @@ func TestTranslateByDLXRejectsOversizedTextsBeforeUpstream(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
length, ok := totalTextLength(tt.texts)
_, length, ok := textsToTranslate(tt.texts)
if !ok {
t.Fatalf("totalTextLength(%q) reported no text", tt.texts)
t.Fatalf("textsToTranslate(%q) reported no text", tt.texts)
}
if length <= maxFreeTextLength {
t.Fatalf("totalTextLength(%q) = %d, which is not over the limit", tt.texts, length)
t.Fatalf("textsToTranslate(%q) = %d, which is not over the limit", tt.texts, length)
}
result, err := TranslateByDLX("", "ZH", tt.texts, "", "", "")
if err != nil {