Skip to content

refactor: P3 code quality improvements across translation services#72

Open
xiaocang wants to merge 1 commit intomasterfrom
claude/refactor-translation-services-NDxHI
Open

refactor: P3 code quality improvements across translation services#72
xiaocang wants to merge 1 commit intomasterfrom
claude/refactor-translation-services-NDxHI

Conversation

@xiaocang
Copy link
Owner

@xiaocang xiaocang commented Feb 6, 2026

  • DoubaoService: replace List + string.Join with StringBuilder for consistency with GeminiService and BaseOpenAIService; rename RemoveSurroundingQuotes to CleanupResult to match naming convention
  • GeminiService: remove unused StringBuilder buffer in ParseGeminiStreamAsync
  • HotkeyService: add volatile modifier to _isDisposed and _isInitialized for defensive consistency with MouseHookService
  • DeepLService: merge duplicate GetDeepLLanguageCode/GetDeepLWebLanguageCode into single method with isWeb parameter (only difference: Portuguese PT vs PT-PT), removing ~28 lines of duplication

https://claude.ai/code/session_01Sz7TonncZGm4PgXYBzPEBo

- DoubaoService: replace List<string> + string.Join with StringBuilder
  for consistency with GeminiService and BaseOpenAIService; rename
  RemoveSurroundingQuotes to CleanupResult to match naming convention
- GeminiService: remove unused StringBuilder buffer in ParseGeminiStreamAsync
- HotkeyService: add volatile modifier to _isDisposed and _isInitialized
  for defensive consistency with MouseHookService
- DeepLService: merge duplicate GetDeepLLanguageCode/GetDeepLWebLanguageCode
  into single method with isWeb parameter (only difference: Portuguese
  PT vs PT-PT), removing ~28 lines of duplication

https://claude.ai/code/session_01Sz7TonncZGm4PgXYBzPEBo
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

Refactors several translation services and a WinUI hotkey service to reduce duplication, remove unused code, and align implementation patterns across providers.

Changes:

  • DoubaoService: accumulate streaming chunks via StringBuilder and rename the post-processing helper to CleanupResult.
  • DeepLService: consolidate API/web language-code mapping into a single method with an isWeb parameter.
  • GeminiService / HotkeyService: remove an unused variable and mark state flags as volatile.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
dotnet/src/Easydict.WinUI/Services/HotkeyService.cs Marks initialization/disposal flags as volatile.
dotnet/src/Easydict.TranslationService/Services/GeminiService.cs Removes an unused StringBuilder in stream parsing.
dotnet/src/Easydict.TranslationService/Services/DoubaoService.cs Switches to StringBuilder for chunk aggregation and refactors/renames result cleanup logic.
dotnet/src/Easydict.TranslationService/Services/DeepLService.cs Removes duplicated language-code mapping by unifying web/API code paths with a parameterized helper.

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

Comment on lines +316 to 321
/// Clean up translated text by trimming and removing surrounding quotes.
/// </summary>
private static string RemoveSurroundingQuotes(string text)
private static string CleanupResult(string text)
{
if (string.IsNullOrEmpty(text))
return text;
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

The new CleanupResult helper claims to "trim" in its XML summary, but its current implementation returns the original untrimmed text when no surrounding quotes are found (so TranslateInternalAsync no longer reliably trims output as it did before). Adjust CleanupResult so the trimming behavior is consistently applied.

Copilot uses AI. Check for mistakes.
Comment on lines +85 to 94
var sb = new StringBuilder();
await foreach (var chunk in TranslateStreamAsync(request, cancellationToken))
{
chunks.Add(chunk);
sb.Append(chunk);
}

var translatedText = string.Join("", chunks).Trim();
translatedText = RemoveSurroundingQuotes(translatedText);
var translatedText = CleanupResult(sb.ToString());

return new TranslationResult
{
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

Consider adding a unit test that covers whitespace-only differences (e.g., a streamed result with trailing newlines/spaces and no surrounding quotes) to ensure CleanupResult/TranslateAsync preserves the intended trimming behavior going forward.

Copilot uses AI. Check for mistakes.
@claude
Copy link

claude bot commented Feb 6, 2026

Code Review

I've reviewed this PR and found 1 bug that needs to be addressed:


Bug: CleanupResult fails to trim when no surrounding quotes are present

Location: DoubaoService.cs:331

Issue:
In the CleanupResult method, line 331 returns the original untrimmed text instead of the trimmed variable when the text does not have surrounding quotes.

var trimmed = text.Trim();                    // Line 323
if (trimmed.Length >= 2 && /* quotes check */)
{
    return trimmed[1..^1];                    // Line 328 - correct
}

return text;  // Line 331 - BUG: should be 'return trimmed;'

Why this is a bug:
The method's XML doc comment states it will "trim and remove surrounding quotes", but the no-quotes path skips trimming. This is inconsistent with:

  1. The method's documented behavior
  2. The identical CleanupResult methods in GeminiService:327-339 and BaseOpenAIService:283-295, which both always return the trimmed text
  3. The pre-refactor code which called .Trim() inline at line 88 before calling RemoveSurroundingQuotes

Impact: Translation results without surrounding quotes will retain leading/trailing whitespace that was previously trimmed.

Fix: Change line 331 from return text; to return trimmed;


All other changes in this PR look good - no CLAUDE.md violations were found.

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.

2 participants