feat(translate): accept an array of texts in POST /translate

`text` now accepts either a single string or an array of strings. A string
is answered with a string in `data` exactly as before; an array is answered
with a position-aligned array, and the whole batch travels in a single
oneshot request, so a caller with many segments needs one round trip
instead of one per segment.

A batch is all-or-nothing: an empty array or an empty element answers with
the existing 404 "No text to translate", and if upstream returns a
different number of translations or leaves one empty the request fails with
503 "Translation failed" instead of returning placeholders or a misaligned
array. The anonymous length cap applies to the total length of the array,
which the README now documents.

Refs #240
This commit is contained in:
JounQin
2026-10-10 11:01:39 +00:00
parent 54158cddd5
commit 97001438d1
6 changed files with 297 additions and 32 deletions
+5 -1
View File
@@ -16,11 +16,15 @@ package translate
// handlers in the service package. The structure predates the migration to
// the iOS oneshot endpoint; Alternatives is now always empty because oneshot
// does not return alternative translations, and ID is synthesized from time.
//
// Data holds one translation per requested text, in request order. The
// handlers render it as a bare string when the caller sent a bare string in
// `text` and as an array of the same length when the caller sent an array.
type DLXTranslationResult struct {
Code int `json:"code"`
ID int64 `json:"id"`
Message string `json:"message,omitempty"`
Data string `json:"data"`
Data []string `json:"data"`
Alternatives []string `json:"alternatives"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`