Skip to content

fix(translate): collect all Baidu trans_result lines instead of first only#10423

Draft
Ironship wants to merge 1 commit intoSubtitleEdit:mainfrom
Ironship:fix/baidu-multiline-10420
Draft

fix(translate): collect all Baidu trans_result lines instead of first only#10423
Ironship wants to merge 1 commit intoSubtitleEdit:mainfrom
Ironship:fix/baidu-multiline-10420

Conversation

@Ironship
Copy link
Contributor

Problem

When input text contains \n, Baidu API splits it into segments and returns one array element per line in trans_result. The previous code read only transResult[0], so everything after the first newline was silently discarded.

Fix

Iterate over all trans_result elements, collect each dst, then rejoin with \n.

Fixes #10420

Related

… 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>
@Ironship Ironship marked this pull request as ready for review March 25, 2026 13:26
Copilot AI review requested due to automatic review settings March 25, 2026 13:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_result elements and collect each dst value.
  • Decode each collected dst via Json.DecodeJsonText and join results back into a single string.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +120 to 128
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));
}
}
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.

var outputText = Json.DecodeJsonText(dst).Trim();
return outputText;
return string.Join("\n", translations).Trim();
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
return string.Join("\n", translations).Trim();
return string.Join(Environment.NewLine, translations).Trim();

Copilot uses AI. Check for mistakes.
@Ironship Ironship marked this pull request as draft March 25, 2026 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A Bug in the Baidu Translate API in SE5.0-beta10

3 participants