C# calls Baidu translation

Calling Baidu Translate requires an API Key. For details, you can go to http://developer.baidu.com/console#app/project  to apply. After applying, you can see the following page:

After getting it, you can call the translation API.

According to Baidu's API documentation: http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3% E9%A6%96%E9%A1%B5/%E7%99%BE%E5%BA%A6%E7%BF%BB%E8%AF%91/%E7%BF%BB%E8%AF%91API

We need to define 2 classes to store the returned results:

    public class TranslationResult
    {
        public string Error_code { get; set; }
        public string Error_msg { get; set; }
        public string From { get; set; }
        public string To { get; set; }
        public string Query { get; set; }
        public Translation[] Trans_result { get; set; }
    }
    public class Translation
    {
        public string Src { get; set; }
        public string Dst { get; set; }
    }
 
 

The first class represents the status and other information of the returned result, and it also contains the real translation result, which is a Translation array. Because the string you need to translate may be multi-line, it is an array, of course, you can also define it as a collection. And the second class represents the source and translation of each line translated.

In addition, the direct return result of Baidu Translate is a JSON type string, so we can deserialize it through the System.Web.Script.Serialization.JavaScriptSerializer class to become the two classes I defined above, which is why I defined There will be strange names like Error_code in the attribute of .

In addition, I also defined an enumeration to store the language:

    public enum Language
    {
        Auto = 0,
        ZH = 1,
        JP = 2,
        IN = 3,
        KOR = 4,
        SPA = 5,
        FRA = 6,
        TH = 7,
        NOW = 8,
        RU = 9,
        PT = 10,
        YUE = 11,
        WYW = 12,
        SD = 13,
        NL = 14,
        IT = 15,
        EL = 16
    }

Finally, we just need to send a request to Baidu Translate:

        public static TranslationResult GetTranslationFromBaiduFanyi(string source, Language from = Language.Auto, Language to = Language.Auto)
        {
            string jsonResult = string.Empty;
            string apiKey = "yourApiKey";
            string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}",
                apiKey,
                HttpUtility.UrlEncode(source, Encoding.UTF8),
                from.ToString().ToLower(),
                to.ToString().ToLower());
            WebClient wc = new WebClient();
            try
            {
                jsonResult = wc.DownloadString(url);
            }
            catch (Exception e)
            {
                jsonResult = string.Empty;
            }
            JavaScriptSerializer jss = new JavaScriptSerializer();
            TranslationResult ret = jss.Deserialize<TranslationResult>(jsonResult);
            return ret;
        }







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325411869&siteId=291194637