fix(translate): collect all Baidu trans_result lines instead of first only#10423
fix(translate): collect all Baidu trans_result lines instead of first only#10423Ironship wants to merge 1 commit intoSubtitleEdit:mainfrom
Conversation
… only When input text contains newlines, Baidu API splits it into segments and returns one trans_result array element per line. The previous code read only transResult[0], discarding all lines after the first \n. Collect all dst values and rejoin with \n. Fixes SubtitleEdit#10420 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes Baidu auto-translate handling for multi-line inputs by collecting all elements returned in the trans_result array instead of only the first, preventing content after the first newline from being dropped.
Changes:
- Iterate over all
trans_resultelements and collect eachdstvalue. - Decode each collected
dstviaJson.DecodeJsonTextand join results back into a single string.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var translations = new List<string>(); | ||
| foreach (var item in transResult) | ||
| { | ||
| return string.Empty; | ||
| var dst = parser.GetFirstObject(item, "dst"); | ||
| if (!string.IsNullOrEmpty(dst)) | ||
| { | ||
| translations.Add(Json.DecodeJsonText(dst)); | ||
| } | ||
| } |
There was a problem hiding this comment.
The loop skips trans_result items where dst is empty, which can change the number/position of lines in the returned translation (e.g., if the input contains blank lines). The merge/split pipeline relies on preserving empty lines when splitting translations back into paragraphs, so this can cause split validation to fail. Consider always adding an entry per trans_result element (use string.Empty when dst is missing/empty), and only apply decoding when non-empty.
|
|
||
| var outputText = Json.DecodeJsonText(dst).Trim(); | ||
| return outputText; | ||
| return string.Join("\n", translations).Trim(); |
There was a problem hiding this comment.
string.Join("\n", translations) introduces a hardcoded LF line separator. Most other translators normalize/join multi-line translations using Environment.NewLine (e.g., DeepLTranslate.cs joins with Environment.NewLine), which keeps newline handling consistent across platforms and downstream split/formatting logic. Consider joining with Environment.NewLine here as well (or normalizing via SplitToLines() before returning).
| return string.Join("\n", translations).Trim(); | |
| return string.Join(Environment.NewLine, translations).Trim(); |
Problem
When input text contains
\n, Baidu API splits it into segments and returns one array element per line intrans_result. The previous code read onlytransResult[0], so everything after the first newline was silently discarded.Fix
Iterate over all
trans_resultelements, collect eachdst, then rejoin with\n.Fixes #10420
Related