Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 6 additions & 38 deletions dotnet/src/Easydict.TranslationService/Services/DeepLService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ private async Task<TranslationResult> TranslateWebAsync(
TranslationRequest request,
CancellationToken cancellationToken)
{
var targetCode = GetDeepLWebLanguageCode(request.ToLanguage);
var targetCode = GetDeepLLanguageCode(request.ToLanguage, isWeb: true);
var sourceCode = request.FromLanguage == Language.Auto
? "auto"
: GetDeepLWebLanguageCode(request.FromLanguage);
: GetDeepLLanguageCode(request.FromLanguage, isWeb: true);

// Generate anti-detection values (matching macOS implementation)
var requestId = GetRandomRequestId();
Expand Down Expand Up @@ -385,9 +385,10 @@ private TranslationResult ParseWebResponse(string json, TranslationRequest reque
}

/// <summary>
/// Get language code for official DeepL API.
/// Get language code for DeepL API or web JSON-RPC.
/// The only difference is Portuguese: API uses "PT", web uses "PT-PT".
/// </summary>
private static string GetDeepLLanguageCode(Language language) => language switch
private static string GetDeepLLanguageCode(Language language, bool isWeb = false) => language switch
{
Language.SimplifiedChinese => "ZH",
Language.TraditionalChinese => "ZH-HANT",
Expand All @@ -396,40 +397,7 @@ private TranslationResult ParseWebResponse(string json, TranslationRequest reque
Language.Korean => "KO",
Language.French => "FR",
Language.Spanish => "ES",
Language.Portuguese => "PT",
Language.Italian => "IT",
Language.German => "DE",
Language.Russian => "RU",
Language.Dutch => "NL",
Language.Polish => "PL",
Language.Bulgarian => "BG",
Language.Czech => "CS",
Language.Danish => "DA",
Language.Finnish => "FI",
Language.Greek => "EL",
Language.Hungarian => "HU",
Language.Indonesian => "ID",
Language.Norwegian => "NB",
Language.Romanian => "RO",
Language.Swedish => "SV",
Language.Turkish => "TR",
Language.Ukrainian => "UK",
_ => language.ToIso639().ToUpper()
};

/// <summary>
/// Get language code for DeepL web JSON-RPC (slightly different format).
/// </summary>
private static string GetDeepLWebLanguageCode(Language language) => language switch
{
Language.SimplifiedChinese => "ZH",
Language.TraditionalChinese => "ZH-HANT",
Language.English => "EN",
Language.Japanese => "JA",
Language.Korean => "KO",
Language.French => "FR",
Language.Spanish => "ES",
Language.Portuguese => "PT-PT",
Language.Portuguese => isWeb ? "PT-PT" : "PT",
Language.Italian => "IT",
Language.German => "DE",
Language.Russian => "RU",
Expand Down
11 changes: 5 additions & 6 deletions dotnet/src/Easydict.TranslationService/Services/DoubaoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ protected override async Task<TranslationResult> TranslateInternalAsync(
TranslationRequest request,
CancellationToken cancellationToken = default)
{
var chunks = new List<string>();
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
{
Comment on lines +85 to 94
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.
Expand Down Expand Up @@ -314,9 +313,9 @@ private static async IAsyncEnumerable<string> ParseDoubaoStreamAsync(
};

/// <summary>
/// Remove surrounding quotes from translated text if present.
/// 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;
Comment on lines +316 to 321
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ private static async IAsyncEnumerable<string> ParseGeminiStreamAsync(
[EnumeratorCancellation] CancellationToken cancellationToken)
{
using var reader = new StreamReader(stream);
var buffer = new StringBuilder();

while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync(cancellationToken);
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/Easydict.WinUI/Services/HotkeyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public sealed class HotkeyService : IDisposable

private readonly Window _window;
private readonly nint _hwnd;
private bool _isDisposed;
private bool _isInitialized;
private volatile bool _isDisposed;
private volatile bool _isInitialized;

// Window subclass delegate - must keep reference to prevent GC
private delegate nint SubclassProc(nint hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nuint dwRefData);
Expand Down
Loading