From 0201fa691a7a1cfb083dd8d94fad5c1817986818 Mon Sep 17 00:00:00 2001 From: SongWJ Date: Thu, 7 Nov 2024 09:53:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=95=BF=E6=96=87=E6=9C=AC=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=E4=B8=A2=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STranslateDLL/LocalMode.cs | 16 +++++++++++++--- STranslateDLLTests/LocalModeTests.cs | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/STranslateDLL/LocalMode.cs b/STranslateDLL/LocalMode.cs index 02c0623..cc3af3c 100644 --- a/STranslateDLL/LocalMode.cs +++ b/STranslateDLL/LocalMode.cs @@ -61,15 +61,25 @@ public static class LocalMode var responseBody = await SendRequestAsync(json, getToken, "LMT_handle_jobs"); var jNode = JsonNode.Parse(responseBody); - var data = jNode?["result"]?["translations"]?[0]?["beams"]?[0]?["sentences"]?[0]?["text"]?.ToString(); + var resultList = jNode?["result"]?["translations"]; + var data = ""; + if (resultList is not JsonArray jsonArray) + return Utilities.Serialize(new Response + { + Code = 500, + Data = "Error: No translation result" + }); + else + data = jsonArray.Aggregate(data, (current, item) => current + item?["beams"]?[0]?["sentences"]?[0]?["text"]); + // data = jNode?["result"]?["translations"]?[0]?["beams"]?[0]?["sentences"]?[0]?["text"]?.ToString(); var errorMsg = jNode?["error"]?["message"]?.ToString(); var detailsMsg = jNode?["error"]?["data"]?["what"]?.ToString(); var error = $"Error: {errorMsg}\nDetails: {detailsMsg}"; return Utilities.Serialize(new Response { - Code = data != null ? 200 : 500, - Data = data ?? error + Code = string.IsNullOrEmpty(data) ? 500 : 200, + Data = string.IsNullOrEmpty(data) ? error : data }); } diff --git a/STranslateDLLTests/LocalModeTests.cs b/STranslateDLLTests/LocalModeTests.cs index b37653b..d2bc2a9 100644 --- a/STranslateDLLTests/LocalModeTests.cs +++ b/STranslateDLLTests/LocalModeTests.cs @@ -13,7 +13,7 @@ public class LocalModeTests(ITestOutputHelper testOutputHelper) try { var cts = new CancellationTokenSource(); - var ret = await LocalMode.ExecuteAsync("Hello World", "auto", "ZH", cts.Token); + var ret = await LocalMode.ExecuteAsync("20241105版本说明:\\r\\n1. 修复STranslate本地服务翻译失败的问题\\r\\n2. 添加生词本(欧路词典)\\r\\n3. 优化设置页面\\r\\n4. 优化查词显示效果\\r\\n5. 添加金山词霸、必应词典作为内置服务\\r\\n6. 添加EdgeTTS作为内置服务,TTS效果更佳\\r\\n7. 添加软件内TTS热键(参见设置-热键设置-软件热键列表)\\r\\n8. 启动时可选不显示通知\\r\\n9. 粘贴文本自动执行翻译\\r\\n---\\r\\n测试项:\\r\\n替换翻译及静默OCR使用鼠标样式作为执行中标记,如若执行结束导致鼠标未恢复,恳请反馈一下,恢复方式到设置中设置鼠标即可https://jingyan.baidu.com/article/6c67b1d6ddbced6686bb1e2c.html\\r\\n\\r\\n开发者个人样本比较少,不知道是否存在不可控因素,这两天大家如果没有问题反馈就发正式版本", "ZH", "EN", cts.Token); testOutputHelper.WriteLine(ret); } catch (Exception ex)