C/S快速开发框架多语言功能集成百度翻译接口(支持中文简体、繁体、英文、越南文)
C/S快速开发框架多语言功能集成百度翻译接口(支持中文简体、繁体、英文、越南文)
BaiduTrans 类:
扫一扫加微信
适用开发:制造、服务、零售、商贸等行业的ERP、MRP、MES、CRM、MIS、HIS、POS数据管理应用系统
运行平台:Windows + .NET Framework 4.5
开发工具:Visual Studio 2015+,C#语言
数据库:Microsoft SQLServer 2008R2+(支持多数据库:Oracle/MySql)
运行平台:Windows + .NET Framework 4.5
开发工具:Visual Studio 2015+,C#语言
数据库:Microsoft SQLServer 2008R2+(支持多数据库:Oracle/MySql)
多语言界面开发框架简介
C/S系统软件快速开发框架支持多国语言界面,语言资料存储在CSFrameworkVXX.System数据库的sys_Language表。目前支持中文繁体、简体、英文、越南文(默认是中文简体),分别对应的字段名称:CHS\CHT\ENG\VN,您可通过系统管理模块的【多语言管理】进行维护语言库资料。
C/S系统软件快速开发框架支持多国语言界面,语言资料存储在CSFrameworkVXX.System数据库的sys_Language表。目前支持中文繁体、简体、英文、越南文(默认是中文简体),分别对应的字段名称:CHS\CHT\ENG\VN,您可通过系统管理模块的【多语言管理】进行维护语言库资料。
BaiduTrans 类:
C# Code:
/// <summary>
/// 翻译内容
/// </summary>
/// <param name="content">内容</param>
/// <param name="from">源语言</param>
/// <param name="to">目标语言</param>
/// <returns></returns>
public string Tran(string content, string from = "zh", string to = "eng")
{
if (String.IsNullOrWhiteSpace(content)) return String.Empty;
//截取左右空格空白字符
content = content.Trim();
//如果使用免费翻译API,访问频率受限1s翻译一次
if (this.IsUseFreeAPI)
{
Application.DoEvents();
Thread.Sleep(1000);
}
//余额不足,不翻译
if (this.IsNoMoney) return content;
string q = content;// 原文
Random rd = new Random();
string salt = rd.Next(100000).ToString();
string appId = _appKey;// 改成您的APP ID
string secretKey = _secretKey;// 改成您的密钥
string sign = EncryptString(appId + q + salt + secretKey);
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "q=" + HttpUtility.UrlEncode(q);
url += "&from=" + from;
url += "&to=" + to;
url += "&appid=" + appId;
url += "&salt=" + salt;
url += "&sign=" + sign;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = 6000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
var data = JsonConvert.DeserializeObject<BaiduTransResult>(retString);
//翻译成功
if (data != null && data.trans_result != null && data.trans_result.Count > 0)
{
var str = data.trans_result[0].dst;
//HTML转译
str = HttpUtility.HtmlDecode(str);
var result = GetResult(content, str);//特殊处理
_TryCount = 0;//翻译成功,计算器重置
return result;
}
else if (data.error_code == "54004")
{
//{"error_code":"54004","error_msg":"Please recharge"}
this.IsNoMoney = true;
//SetFreeAccount();
}
else if (data.error_code == "54003")
{
//54003,Invalid Access Limit
//SetFreeAccount1();
//流量限制,尝试最多翻译3次
_TryCount++;
if (_TryCount < 5)
return Tran(content);
}
else if (!String.IsNullOrEmpty(data.error_msg))
throw new Exception($"调用百度翻译API发生错误:{data.error_code},{data.error_msg}");
_TryCount = 0;
return content;
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// <summary>
/// 翻译内容
/// </summary>
/// <param name="content">内容</param>
/// <param name="from">源语言</param>
/// <param name="to">目标语言</param>
/// <returns></returns>
public string Tran(string content, string from = "zh", string to = "eng")
{
if (String.IsNullOrWhiteSpace(content)) return String.Empty;
//截取左右空格空白字符
content = content.Trim();
//如果使用免费翻译API,访问频率受限1s翻译一次
if (this.IsUseFreeAPI)
{
Application.DoEvents();
Thread.Sleep(1000);
}
//余额不足,不翻译
if (this.IsNoMoney) return content;
string q = content;// 原文
Random rd = new Random();
string salt = rd.Next(100000).ToString();
string appId = _appKey;// 改成您的APP ID
string secretKey = _secretKey;// 改成您的密钥
string sign = EncryptString(appId + q + salt + secretKey);
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "q=" + HttpUtility.UrlEncode(q);
url += "&from=" + from;
url += "&to=" + to;
url += "&appid=" + appId;
url += "&salt=" + salt;
url += "&sign=" + sign;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = 6000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
var data = JsonConvert.DeserializeObject<BaiduTransResult>(retString);
//翻译成功
if (data != null && data.trans_result != null && data.trans_result.Count > 0)
{
var str = data.trans_result[0].dst;
//HTML转译
str = HttpUtility.HtmlDecode(str);
var result = GetResult(content, str);//特殊处理
_TryCount = 0;//翻译成功,计算器重置
return result;
}
else if (data.error_code == "54004")
{
//{"error_code":"54004","error_msg":"Please recharge"}
this.IsNoMoney = true;
//SetFreeAccount();
}
else if (data.error_code == "54003")
{
//54003,Invalid Access Limit
//SetFreeAccount1();
//流量限制,尝试最多翻译3次
_TryCount++;
if (_TryCount < 5)
return Tran(content);
}
else if (!String.IsNullOrEmpty(data.error_msg))
throw new Exception($"调用百度翻译API发生错误:{data.error_code},{data.error_msg}");
_TryCount = 0;
return content;
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
Baidu翻译的英文界面效果:
Baidu翻译的英文界面效果:
扫一扫加微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网