.NET应用系统的国际化-多语言翻译服务


上篇文章我们介绍了

.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