Przeglądaj źródła

!1719 意大利语、法语等机翻常出现"'",判断机翻结果是否存在"'" 如存在在其之前加转义符
Merge pull request !1719 from 莫闻啼/N/A

zuohuaijun 1 rok temu
rodzic
commit
46753af8f3

+ 40 - 6
Admin.NET/Admin.NET.Core/Service/ExtendService/BaiDuTranslationService.cs

@@ -181,11 +181,28 @@ public class BaiDuTranslationService : IDynamicApiController, ITransient
 
                         var result = await Translation("zh", targetLang, $"{gen.Value}");
 
-                        if (result.error_code.Equals("0"))
+
+                        if (!result.error_code.Equals("0"))
+                        {
+                            continue;
+                        }
+
+                        var translationValue = result.trans_result[0].Dst;
+                        LogTranslationProgress(gen.Key, gen.Value, translationValue, ConsoleColor.DarkMagenta);
+
+                        // 如果翻译结果为空字符串不追加
+                        if (string.IsNullOrEmpty(translationValue))
+                        {
+                            continue;
+                        }
+
+                        // 如果翻译结果包含"'" 法语意大利语常出现  在"'"前加转义符
+                        if (translationValue.Contains("'"))
                         {
-                            LogTranslationProgress(gen.Key, gen.Value, result.trans_result[0].Dst, ConsoleColor.DarkMagenta);
-                            str += ($"        {gen.Key}: '{result.trans_result[0].Dst}',{Environment.NewLine}");
+                            translationValue = translationValue.Replace("'", "\\'");
                         }
+                        
+                        str += ($"        {gen.Key}: '{translationValue}',{Environment.NewLine}");
                     }
                     catch (Exception e)
                     {
@@ -270,11 +287,28 @@ public class BaiDuTranslationService : IDynamicApiController, ITransient
 
                         var result = await Translation("zh", targetLang, $"{gen.Value}");
 
-                        if (result.error_code.Equals("0"))
+                        
+                        if (!result.error_code.Equals("0"))
                         {
-                            LogTranslationProgress(gen.Key, gen.Value, result.trans_result[0].Dst, ConsoleColor.DarkMagenta);
-                            str += ($"        {gen.Key}: '{result.trans_result[0].Dst}',{Environment.NewLine}");
+                            continue;
+                        }
+
+                        var translationValue = result.trans_result[0].Dst;
+                        LogTranslationProgress(gen.Key, gen.Value, translationValue, ConsoleColor.DarkMagenta);
+
+                        // 如果翻译结果为空字符串不追加
+                        if (string.IsNullOrEmpty(translationValue))
+                        {
+                            continue;
                         }
+
+                        // 如果翻译结果包含"'" 法语意大利语常出现  在"'"前加转义符
+                        if (translationValue.Contains("'"))
+                        {
+                            translationValue = translationValue.Replace("'", "\\'");
+                        }
+
+                        str += ($"        {gen.Key}: '{translationValue}',{Environment.NewLine}");
                     }
                     catch (Exception e)
                     {