.NET應用系統的國際化-多語言翻譯服務

2023-03-20 09:03:35

上篇文章我們介紹了

.NET應用系統的國際化-基於Roslyn抽取詞條、更新程式碼

系統國際化改造整體設計思路如下:

  1. 提供一個工具,識別前後端程式碼中的中文,形成多語言詞條,按語言、介面、模組統一管理多有的多語言詞條
  2. 提供一個翻譯服務,批次翻譯多語言詞條
  3. 提供一個詞條服務,支援後端程式碼在執行時根據使用者登入的語言,動態獲取對應的多語言文字
  4. 提供前端多語言JS生成服務,按介面動態生成對應的多語言JS檔案,方便前端VUE檔案使用。
  5. 提供程式碼替換工具,將VUE前端程式碼中的中文替換為$t("詞條ID"),後端程式碼中的中文替換為TermService.Current.GetText("詞條ID")

本篇文章我們重點和大家分享多語言翻譯服務的設計和實現。

一、業務背景

通過上一篇文章,我們把sln解決方案中各個Project下的中文文字,識別成大量的多語言詞條。

這些多語言詞條臨時儲存在資料庫中,我們要對這個臨時結果集,通過多語言翻譯服務,按支援的語言,翻譯成多語言詞條。

對應的類圖設計:

 

 

 對應的詞條管理介面:

 

 

 因此我們需要一個多語言詞條翻譯服務,實現詞條的批次、快速機器翻譯。

二、多語言詞條翻譯服務

首先,抽象一個翻譯介面II18NTermTranslateService

/// <summary>
/// 詞條翻譯服務介面
/// </summary>
public interface II18NTermTranslateService
{
     string Translate(string text, string language);
}

設計一個翻譯服務提供者類,通過Facade模式,對外統一提供翻譯服務TranslateServiceProvider

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using I18N.SPI;

namespace I18N.Translation
{
    /// <summary>
    /// 翻譯服務提供者
    /// </summary>
    public class TranslateServiceProvider
    {
        public static II18NTermTranslateService GetTranslateService(Translater translater)
        {
            switch (translater)
            {
                case Translater.Youdao:
                default:
                    return new YoudaoTranslateService();
                case Translater.Baidu:
                    return new BaiduTranslateService();
                case Translater.Google:
                    return new GoogleTranslateService();
                case Translater.Azure:
                    return new AzureTranslateService();
            }
        }

        public static II18NTermTranslateService GetYoudaoTranslateService()
        {
            return new YoudaoTranslateService();
        }

        public static II18NTermTranslateService GetGoogleTranslateService()
        {
            return new GoogleTranslateService();
        }

        public static II18NTermTranslateService GetBaiduTranslateService()
        {
            return new BaiduTranslateService();
        }

        public static II18NTermTranslateService GetAzureTranslateService()
        {
            return new AzureTranslateService();
        }
    }
}

 這裡的Translater是個列舉

public enum Translater
    {
        Youdao,
        Baidu,
        Google,
        Azure
    }

三、多語言詞條翻譯服務-Azure翻譯服務

這裡我們使用Azure認知服務中的服務服務,實現上面抽象好的翻譯介面

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using I18N.SPI;

namespace I18N.Translation
{
    /// <summary>
    /// Azure翻譯服務
    /// </summary>
    /// <remarks>
    /// https://learn.microsoft.com/zh-cn/azure/cognitive-services/translator/text-translation-overview
    /// </remarks>
    public class AzureTranslateService : II18NTermTranslateService
    {
        private readonly string _endpoint = "https://api.cognitive.microsofttranslator.com";
        private readonly string _key = "XXXXXXXXXXXXXX";
        public string Translate(string text, string language)
        {
            return Post(text, language);
        }

        private string Post(string text, string language)
        {
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage())
                {
                    var url = $"/translate?api-version=3.0&to={language}";
                    request.Method = HttpMethod.Post;
                    request.RequestUri = new Uri($"{_endpoint}{url}");
                    object[] body = { new { Text = text } };
                    var requestBody = JsonConvert.SerializeObject(body);
                    request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                    request.Headers.Add("Ocp-Apim-Subscription-Key", _key);

                    var response = client.SendAsync(request).ConfigureAwait(false).GetAwaiter().GetResult();
                    if (!response.IsSuccessStatusCode)
                    {
                        return null;
                    }
                    string result = response.Content.ReadAsStringAsync().Result;
                    var translationResults = JsonConvert.DeserializeObject<TranslationResult[]>(result);
                    if (translationResults.Length > 0)
                    {
                        return translationResults[0].Translations.FirstOrDefault()?.Text;
                    }
                }
            }
            return null;
        }
    }
}

這裡用到了幾個引數類

namespace I18N.Translation
{
    public class TranslationResult
    {
        public DetectedLanguage DetectedLanguage { get; set; }
        public Translation[] Translations { get; set; }
    }

    public class DetectedLanguage
    {
        public string Language { get; set; }
        public float Score { get; set; }
    }
    public class Translation
    {
        public string Text { get; set; }
        public string To { get; set; }
    }
}

四、多語言詞條翻譯服務-有道雲翻譯服務

這裡我們同時實現了有道雲翻譯服務

using System;
using System.Linq;
using System.Collections.Generic;
using System.Net.Http;
using System.Web;
using I18N.SPI;

namespace I18N.Translation
{
    /// <summary>
    /// 有道雲翻譯服務
    /// </summary>
    /// <remarks>
    /// https://ai.youdao.com/DOCSIRMA/html/%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E7%BF%BB%E8%AF%91/API%E6%96%87%E6%A1%A3/%E6%96%87%E6%9C%AC%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1/%E6%96%87%E6%9C%AC%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1-API%E6%96%87%E6%A1%A3.html
    /// </remarks>
    public class YoudaoTranslateService : II18NTermTranslateService
    {
        private string appKey = "XXXXXX";
        private string appKeyMY = "XXXXXXXXX";

        public string Translate(string text, string language)
        {
            return Post(text, language);
        }

        public void Translate(List<I18NTerm> terms, string language)
        { }

        private string Post(string text, string language)
        {
            var salt = ToUnixTime(DateTime.Now);
            var sign = Encryptor.MD5Hash(appKey + text + salt + appKeyMY).ToUpper();

            switch (language.ToLower())
            {
                case "zh-cn": language = "zh-CHS"; break;
                case "en-us": language = "EN"; break;
            }

            HttpClient client = new HttpClient();

            var encodedText = System.Uri.EscapeUriString(text);
            var url = @"https://openapi.youdao.com/api?q=" + encodedText + "&from=auto&to=" + language + "&appKey=" + appKey + "&salt=" + salt + "&sign=" + sign;            
            var result = client.GetStringAsync(url).Result;

            //{"returnPhrase":["系統"],"query":"系統","errorCode":"0","l":"zh-CHS2en","tSpeakUrl":"https://openapi.youdao.com/ttsapi?q=system&langType=en&sign=F1945F1CB2D0AEEE40B1277E6C871770&salt=1665580681639&voice=4&format=mp3&appKey=48045ce9f1d5f934&ttsVoiceStrict=false","web":[{"value":["System","lineage","Systematic problem-solving","Windows XP"],"key":"系統"},{"value":["Operating System","OS","Linux"],"key":"作業系統"},{"value":["Domain Name System","Domain Name Server","Domain System"],"key":"域名系統"}],"requestId":"cf134fc6-812b-49ab-a97a-85e56e6697cd","translation":["system"],"dict":{"url":"yddict://m.youdao.com/dict?le=eng&q=%E7%B3%BB%E7%BB%9F"},"webdict":{"url":"http://mobile.youdao.com/dict?le=eng&q=%E7%B3%BB%E7%BB%9F"},"basic":{"phonetic":"xì tǒng","explains":["system"]},"isWord":true,"speakUrl":"https://openapi.youdao.com/ttsapi?q=%E7%B3%BB%E7%BB%9F&langType=zh-CHS&sign=DF2CDF4E306FC8C4F8E224C6E7436B26&salt=1665580681639&voice=4&format=mp3&appKey=48045ce9f1d5f934&ttsVoiceStrict=false"}
            var dataResult = Newtonsoft.Json.JsonConvert.DeserializeObject<YoudaoResult>(result);
            if (dataResult != null)
            {
                return dataResult.translation.FirstOrDefault();
            }

            return null;
        }

        private long ToUnixTime(DateTime dateTime)
        {
            var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

            return Convert.ToInt64((dateTime.ToUniversalTime() - start).TotalMilliseconds);
        }
    }
}

有道雲的翻譯HttpAPI涉及到了幾個引數類

  public class YoudaoResult
    {
        /// <summary>
        /// 錯誤返回碼
        /// </summary>
        public string errorCode { get; set; }

        /// <summary>
        /// 源語言和目標語言
        /// </summary>
        public string l { get; set; }

        /// <summary>
        /// 源語言
        /// </summary>
        public string query { get; set; }

        /// <summary>
        /// 源語言
        /// </summary>
        public List<string> translation { get; set; }
    }

 

以上是和大家分享多語言翻譯服務的設計和實現。

 

周國慶

2023/3/19