csharp:百度翻译
参考:http://api.fanyi.baidu.com/api/trans/product/index
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%91API
winform:
/// <summary>
/// 涂聚文
/// </summary>
public partial class Form5 : Form
{
TranClass tranClass = new TranClass();
string url = "";
string youAPI_Id = "geovindu";
/// <summary>
///
/// </summary>
/// <returns></returns>
DataTable setLanguage()
{ DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(1, "中文--英文");
dt.Rows.Add(2, "英文--中文");
dt.Rows.Add(3, "中文--日文");
dt.Rows.Add(4, "日文--中文");
dt.Rows.Add(5, "中文--韩文");
dt.Rows.Add(6, "韩文--中文");
dt.Rows.Add(7, "西班牙文--中文");
dt.Rows.Add(8, "中文--西班牙文");
return dt; }
/// <summary>
///
/// </summary>
public Form5()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form5_Load(object sender, EventArgs e)
{
this.cbxTranslateChange.DataSource = setLanguage();
this.cbxTranslateChange.ValueMember = "id";
this.cbxTranslateChange.DisplayMember = "name"; }
/// <summary>
/// 翻译
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
WebClient client = new WebClient(); //引用System.Net
string fromTranslate = tbxFromContent.Text; //翻译前的内容
if (!string.IsNullOrEmpty(fromTranslate))
{
//client_id为自己的api_id,q为翻译对象,from为翻译语言,to为翻译后语言
string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", youAPI_Id, fromTranslate, tranClass.From, tranClass.To);
var buffer = client.DownloadData(url);
string result = Encoding.UTF8.GetString(buffer);
StringReader sr = new StringReader(result);
JsonTextReader jsonReader = new JsonTextReader(sr); //引用Newtonsoft.Json 自带
JsonSerializer serializer = new JsonSerializer();
var r = serializer.Deserialize<TranClass>(jsonReader); //因为获取后的为json对象 ,实行转换
tbxToContent.Text = r.Trans_result[0].dst; //dst为翻译后的值
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbxTranslateChange_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
switch (comboBox.SelectedIndex)
{
case 1:
tranClass.From = "zh";
tranClass.To = "en";
break;
case 2:
tranClass.From = "en";
tranClass.To = "zh";
break;
case 3:
tranClass.From = "zh";
tranClass.To = "jp";
break;
case 4:
tranClass.From = "jp";
tranClass.To = "zh";
break;
case 5:
tranClass.From = "zh";
tranClass.To = "kor";
break;
case 6:
tranClass.From = "kor";
tranClass.To = "zh";
break;
case 7:
tranClass.From = "zh";
tranClass.To = "spa";
break;
case 8:
tranClass.From = "spa";
tranClass.To = "zh";
break;
default:
tranClass.From = "auto";
tranClass.To = "auto";
break; }
} } public class TranClass
{
public string From { get; set; }
public string To { get; set; }
public List<Trans_result> Trans_result { get; set; }
}
public class Trans_result
{
public string src { get; set; }
public string dst { get; set; }
} /* 语言简写 名称
auto 自动检测
zh 中文
en 英语
yue 粤语
wyw 文言文
jp 日语
kor 韩语
fra 法语
spa 西班牙语
th 泰语
ara 阿拉伯语
ru 俄语
pt 葡萄牙语
de 德语
it 意大利语
el 希腊语
nl 荷兰语
pl 波兰语
bul 保加利亚语
est 爱沙尼亚语
dan 丹麦语
fin 芬兰语
cs 捷克语
rom 罗马尼亚语
slo 斯洛文尼亚语
swe 瑞典语
hu 匈牙利语
cht 繁体中文
*/
}
webform:
TranClass tranClass = new TranClass();
string url = "www.dusystem.com";
string youAPI_Id = "geovindu";
/// <summary>
/// 涂聚文
/// </summary>
/// <returns></returns>
DataTable setLanguage()
{ DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(1, "中文--英文");
dt.Rows.Add(2, "英文--中文");
dt.Rows.Add(3, "中文--日文");
dt.Rows.Add(4, "日文--中文");
dt.Rows.Add(5, "中文--韩文");
dt.Rows.Add(6, "韩文--中文");
dt.Rows.Add(7, "西班牙文--中文");
dt.Rows.Add(8, "中文--西班牙文");
return dt; }
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DropDownList1.DataSource = setLanguage();
this.DropDownList1.DataTextField = "name";
this.DropDownList1.DataValueField = "id";
this.DropDownList1.DataBind();
//DropDownList1.SelectedIndex = 0;
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
int k = int.Parse(DropDownList1.SelectedValue);
switch (k)
{
case 1:
tranClass.From = "zh";
tranClass.To = "en";
break;
case 2:
tranClass.From = "en";
tranClass.To = "zh";
break;
case 3:
tranClass.From = "zh";
tranClass.To = "jp";
break;
case 4:
tranClass.From = "jp";
tranClass.To = "zh";
break;
case 5:
tranClass.From = "zh";
tranClass.To = "kor";
break;
case 6:
tranClass.From = "kor";
tranClass.To = "zh";
break;
case 7:
tranClass.From = "zh";
tranClass.To = "spa";
break;
case 8:
tranClass.From = "spa";
tranClass.To = "zh";
break;
default:
tranClass.From = "auto";
tranClass.To = "auto";
break; } WebClient client = new WebClient(); //引用System.Net
string fromTranslate = this.TextBox1.Text; //翻译前的内容 if (object.Equals(tranClass, null))
{
tranClass.From = "zh";
tranClass.To = "en";
} if (!string.IsNullOrEmpty(fromTranslate))
{
//client_id为自己的api_id,q为翻译对象,from为翻译语言,to为翻译后语言
string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", youAPI_Id, fromTranslate, tranClass.From, tranClass.To);
var buffer = client.DownloadData(url);
string result = Encoding.UTF8.GetString(buffer);
StringReader sr = new StringReader(result);
JsonTextReader jsonReader = new JsonTextReader(sr); //引用Newtonsoft.Json 自带
while (jsonReader.Read())
{
//Response.Write(jsonReader.Value.ToString() + "<br/>");//jsonReader.TokenType.ToString() + "" + jsonReader.ValueType.ToString() + "" +
}
Response.Write(CallInterface(fromTranslate, tranClass.From, tranClass.To));
var j = JsonConvert.DeserializeObject<TranClass>(result); //JsonSerializer serializer = new JsonSerializer(); //TranClass p1 = (TranClass)serializer.Deserialize(new JsonTextReader(sr), typeof(TranClass));
//TranClass r = serializer.Deserialize<TranClass>(jsonReader); //因为获取后的为json对象 ,实行转换 反序列化 错误
//this.TextBox2.Text = r.Trans_result[0].dst; //dst为翻译后的值
if (!object.Equals(j, null))
{
this.TextBox2.Text = j.Trans_result[0].dst;
}
}
} /// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{ switch (DropDownList1.SelectedIndex)
{
case 0:
tranClass.From = "zh";
tranClass.To = "en";
break;
case 1:
tranClass.From = "en";
tranClass.To = "zh";
break;
case 2:
tranClass.From = "zh";
tranClass.To = "jp";
break;
case 3:
tranClass.From = "jp";
tranClass.To = "zh";
break;
case 4:
tranClass.From = "zh";
tranClass.To = "kor";
break;
case 5:
tranClass.From = "kor";
tranClass.To = "zh";
break;
case 6:
tranClass.From = "zh";
tranClass.To = "spa";
break;
case 7:
tranClass.From = "spa";
tranClass.To = "zh";
break;
default:
tranClass.From = "auto";
tranClass.To = "auto";
break; }
}
/// <summary>
/// 请求百度接口
///
/// https://github.com/lbv/litjson
/// </summary>
/// <param name="sub">要翻译的文本</param>
/// <param name="from">from语言</param>
/// <param name="to">翻译为语言</param>
/// <returns></returns>
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}", youAPI_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;
}
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
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;
} } /// <summary>
///
/// </summary>
public class TranClass
{
public string From { get; set; }
public string To { get; set; }
public List<Trans_result> Trans_result { get; set; }
}
/// <summary>
///
/// </summary>
public class Trans_result
{
/// <summary>
/// 原文
/// </summary>
public string src { get; set; }
/// <summary>
/// 翻译后的值
/// </summary>
public string dst { get; set; }
} /*
{"from":"zh","to":"en","trans_result":[{"src":"\u4e2d\u56fd","dst":"China"}]}
*/
}
csharp:百度翻译的更多相关文章
- Android开发学习之路-机器学习库(图像识别)、百度翻译
对于机器学习也不是了解的很深入,今天无意中在GitHub看到一个star的比较多的库,就用着试一试,效果也还行.比是可能比不上TensorFlow的,但是在Android上用起来比较简单,毕竟Tens ...
- C# 调用百度翻译Api
这是简单的界面.用的是wpf,winform也可以 具体的操作类 public partial class MainWindow : Window { string url = "" ...
- 基于百度翻译API开发属于自己的翻译工具
你是否每天使用着网页翻译工具?你是否遇到过这种情况,上网过程中遇到一个很长的单词但是又不能复制,要开两个浏览器,一个打开百度翻译,照着另一个网页输入单词?你安装了各种翻译软件后,又删除,只因忍受不了那 ...
- Python 调用百度翻译API
由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...
- 百度翻译&&金山词霸API
#/usr/bin/env python3 #coding=utf8 """百度翻译api功能实现函数,本模块基于Python3.x实现,getTransResult(q ...
- 百度翻译word-wrap,页面错乱原因查找过程(已修复)
今天群里有人发问, 进入百度翻译http://fanyi.baidu.com/#auto/zh/, 输入word-wrap,发现页面错乱. 寻找错乱原因. 上图 开始查找原因: 1.从请求入手 从ch ...
- 百度翻译API
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...
- Asp.NET调用百度翻译
Asp.NET调用百度翻译,图示: HTML: <%@ Page Language="C#" AutoEventWireup="true" CodeFil ...
- WP8.1小梦词典开发2:百度翻译API使用
原文出自:http://www.bcmeng.com/api2/ 小梦昨天和大家分享了WP8.1金山词霸API使用方法,今天继续分享windows phone 8.1中百度翻译API的使用方法.和昨天 ...
随机推荐
- hexdump—Linux系统的二进制文件查看工具
hexdump 无参: 相当于 hexdump -x 0000000 457f 464c 0102 0001 0000 0000 0000 0000 0000010 0002 003e 0001 00 ...
- Rxlifecycle(一):使用
Rxlifecycle使用非常方便简单,如下: 1.集成 build.gradle添加 //Rxlifecycle compile 'com.trello:rxlifecycle:0.3.1' com ...
- Java开发者值得关注的7款新工具
云计算.大数据地快速发展催生了不少热门的应用及工具.作为老牌语言Java,其生态圈也出来了一些有关云服务.监控.文档分享方面的工具.本文总结了7款较新的Java工具,大家不妨看下. 1. JClari ...
- 条件注释判断IE浏览器
最近在用jquery 2.0 才知道已不支持IE6/7/8 但又不想换回 jquery 1.X; 找了一资料发现条件注释可以解决这个问题 这个也像程序中的条件判断,先来介绍几个单词lt :Less t ...
- 关于 Visual Studio 调试 Global 的一点总结
在开发 MVC 的项目中遇到了些问题,想通过调戏查看找问题的原因,发现无法调试 Global 中的 Application_Start 方法,在网上找遍了也没有相应的解决办法,在经过了很多次尝试之后仍 ...
- Ubuntu 16.04 LTS更新
Canonical今天正式发布了新版的Ubuntu系统,针对PC.笔记本.上网本.平板和智能手机各类设备.这次的Ubuntu 16.04代号为Xenial Xerus——这个代号是由Canonical ...
- 记录一个Word操作技巧,很偏门的,鉴于Google很不方便用了,百度起来比较费劲所以记录一下
拿到一篇文章需要修改时需要将文中某一段带有特定文字的段落删除,比如一段带有“淘宝网”文字的广告性宣传,且这种段落并不是全都一样,数量也很多,不太可能手动一段一段找到Delete,这就可以用这个替换查找 ...
- Cloning EBS from Linux 5 to Linux 6 Fails: "Error While Loading Shared Libraries: libclntsh.so.10.1
SYMPTOMS During clone Oracle Applications R12 from Linux 5 to Linux 6 the following error occurs ...
- BAPI 调用相当于BAPI_TRANSACTION_COMMIT 的方法
为什么.net调用SAP的BAPI接口需要调用BAPI_TRANSACTION_COMMIT呢?首先得明白BAPI_TRANSACTION_COMMIT这个BAPI的作用.它功劳很大,在SAP里面很多 ...
- REHL5.5 linux的postfix的邮件服务器配置 (笔记)
一.发送邮件服务器(smtp服务器) 1.系统安装时已经有postfix. 2.修改配置 1)vi main.cf //你可以先备份一下配置文件 myhostname = INMSC2//修改为你的主 ...