c#: Using Baidu Translate API

First, register on the Baidu Translate open platform , and then get two key values: App Id and Secret key , with which you can write programs directly.

When writing programs, refer to the development manual

Because the final return result is like this

{"from":"en","to":"zh","trans_result":[{"src":"hello","dst":"\u4f60\u597d"}]}

First define a Translation.cs with trans_result stored in the result

public class Translation
{
    public string Src { get; set; }
    public string Dst { get; set; }
}

Then define a TranslationResult to store the deserialized result of the above result

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; }
}

After that is the main function: concatenate
the
result 2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4
is Baidu translation website + string to be translated + source language + destination language + appid + random number + md5 check value
This may change, anyway, these help documents are very detailed

public static TranslationResult GetTranslationFromBaiduFanyi(String source, Language from = Language.Auto, Language to = Language.Auto)
{
    String jsonResult = String.Empty;
    String languageFrom = from.ToString().ToLower();
    String languageTo = to.ToString().ToLower();
    String appId = "123321123321";
    String randomNum = System.DateTime.Now.Millisecond.ToString();
    String md5Sign = GetMD5HashFromFile(appId + source + randomNum + "09090909090909");
    Console.WriteLine(appId + source + randomNum + "Cxdi54XVRAnuMAbOjCMr");
    String url = String.Format("http://api.fanyi.baidu.com/api/trans/vip/translate?q={0}&from={1}&to={2}&appid={3}&salt={4}&sign={5}",
        HttpUtility.UrlEncode(source, Encoding.UTF8),
        languageFrom,
        languageTo,
        appId,
        randomNum,
        md5Sign
        );
    Console.WriteLine(url);
    WebClient wc = new WebClient();
    try
    {
        jsonResult = wc.DownloadString(url);
    }
    catch (Exception e)
    {
        jsonResult = string.Empty;
        Console.WriteLine(e.Message);
    }
    JavaScriptSerializer jss = new JavaScriptSerializer();
    TranslationResult ret = jss.Deserialize<TranslationResult>(jsonResult);
    return ret;
}

The MD5 used above can be found on my previous blog or on the Internet

An enumeration definition language type is also used

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

when called

private static void Main(String[] args)
{
    TranslationResult result = TranslateUtil.GetTranslationFromBaiduFanyi("hello", TranslateUtil.Language.EN, TranslateUtil.Language.ZH);
    for (int i = 0; i < result.Trans_result.Length; i++)
    {
        Console.WriteLine(result.Trans_result[i].Dst);
    }
}

Guess you like

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