c#语言程序翻译软件,C#使用百度翻译API

API网址:

http://developer.baidu.com/wiki/index.php?title=帮助文档首页/百度翻译/翻译API

核心代码:

///

/// 请求百度接口

///

/// 要翻译的文本

/// from语言

/// 翻译为语言

///

static private string CallInterface(string sub, string from, string to)

{

HttpContext.Current.Application.Lock();

if (HttpContext.Current.Application["baidu"] == null)

{

HttpContext.Current.Application["baidu"] = 1;

}

else

{

HttpContext.Current.Application["baidu"] = (int)HttpContext.Current.Application["baidu"] + 1;

}

HttpContext.Current.Application.UnLock();

string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", CLIENT_ID, sub, from, to);

string down = "", result = "";

try

{

WebClient wc = new WebClient();

down = wc.DownloadString(url);

}

catch (Exception)

{

}

JsonData jd = JsonMapper.ToObject(down);

if (JsonDataContainsKey(jd, "error_code"))

{

result += sub;

}

else

{

JsonData jdResult = jd["trans_result"];

for (int j = 0; j < jdResult.Count; j++)

{

JsonData jdDst = jdResult[j]["dst"];

result += jdDst.ToString();

}

}

return result;

}

static public bool JsonDataContainsKey(JsonData data, string key)

{

bool result = false;

if (data == null)

return result;

if (!data.IsObject)

{

return result;

}

IDictionary tdictionary = data as IDictionary;

if (tdictionary == null)

return result;

if (tdictionary.Contains(key))

{

result = true;

}

return result;

}

支持的翻译方向:

from字段

to字段

翻译方向

auto

auto

自动识别

zh

en

中 -> 英

zh

jp

中 -> 日

语种编码

目前支持13种语言,如下所示:

语种

代码

语种

代码

中文

zh

英语

en

日语

jp

韩语

kor

西班牙语

spa

法语

fra

泰语

th

阿拉伯语

ara

俄罗斯语

ru

葡萄牙语

pt

粤语

yue

文言文

wyw

白话文

zh

自动检测

auto

德语

de

意大利语

it JsonData使用了LitJSON库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值