Multi-lingual translation using Baidu translation API

Source: Internet
Author: User

Supported languages:

China, UK, China, Japan, Japan, China, South Korea, South Korea, China, France, China, the west, China, Thailand, Thailand, China, Afghanistan, China, Russia, China, Britain, Japan, Barclaycard, Ty, British, Ying, Anglo-Western, matters, Anglo-Portuguese, Portuguese-British
Frequency limit:

For ordinary developers1000 times/hourLimit, support expansion;
Get Request Method:
Http://openapi.baidu.com/public/2.0/bmt/translate?client_id=YourApiKey&q=today&from=auto&to=auto
Response Example:
{"from": "en", "to": "zh", "trans_result": [{"src": "Today", "DST": "\u4eca\u5929"}]}
currently supported in 11 languages, as follows:
ChineseZhEnglishEn
JapaneseJpKoreanKor
SpanishSpaFrenchFra
ThaiThArabicAra
Russian languageRuPortuguesePt
CantoneseYueClassicalWyw
VernacularZhAutomatically detectAuto
steps:
Step1. Application for Apikey,
Http://developer.baidu.com/console#app/project
Step2. Make a GET request
Step3. Get a response
Step4. Parsing the response (critical)
Step5. Result output

Key Code (C #):
public string gettraslation (string src)
{
Send Request
HttpWebRequest Myreq = (HttpWebRequest) webrequest.create ("Http://openapi.baidu.com/public/2.0/bmt/translate? Client_id= "+ client_id +" &q= "+ src +" &from= "+ from +" &to= "+ to);

Get a response
string res = string. Empty;
Try
{
Get response Flow
HttpWebResponse response = (HttpWebResponse) myreq.getresponse ();
StreamReader reader = new StreamReader (response. GetResponseStream (), Encoding.UTF8);
res = reader. ReadToEnd ();
Reader. Close ();
Response. Close ();

Regular expressions
String pattern = @ "" "DST" ":" "(?. *?)""}";
MatchCollection matchs;
string result = String. Empty;
Matchs = regex.matches (res, pattern, Regexoptions.ignorecase | Regexoptions.singleline);
foreach (Match m in Matchs)
{
String strtokenvalue = m.groups["Tokenval"]. Value;
string[] stringseparators = new string[] {"\\u"};
string[] Unicodearray = Strtokenvalue.split (stringseparators, stringsplitoptions.removeemptyentries);
StringBuilder sb = new StringBuilder ();
if (unicodearray.length <= 1)
{
result = Strtokenvalue;
}
Else
{
foreach (string item in Unicodearray)
{
Byte[] codes = new byte[2];
int Code1, Code2;
Code1 = Convert.ToInt32 (item. Substring (0, 2), 16);
Code2 = Convert.ToInt32 (item. Substring (2), 16);
Codes[0] = (byte) code2;//must be small end in front
CODES[1] = (byte) code1;
Sb. Append (Encoding.Unicode.GetString (codes));
}
result = sb. ToString ();
}
}
return result;
}
catch (Exception)
{
return null;
}
}

Key code (JAVA):

public string GetResult (string input) {
try {
String src = urlencoder.encode (input, "utf-8");
HttpGet = new HttpGet ("http://openapi.baidu.com/public/2.0/bmt/translate?client_id=" +client_id+ "&q=" +src+ " &from= "+from+" &to= "+to);
HttpResponse = Httpclient.execute (HttpGet);
Httpentity = Httpresponse.getentity ();
String content = entityutils.tostring (httpentity, "utf-8");
Matcher Matcher = pattern.compile ("\" dst\ ": \" (?. *?) \ "}"). Matcher (content);

if (Matcher.find ()) {
Strtokenvalue = Matcher.group ("Tokenval");
string[] Sarray = Strtokenvalue.split ("\\\\u");
if (sarray.length <= 1) {
result = Strtokenvalue;
}
else {
MyByte = new Byte[2*sarray.length-2];
for (int i = 0; i < sarray.length-1; i++) {
Mybyte[2*i] = (byte) integer.parseint (sarray[i+1].substring (0, 2), 16);
Mybyte[2*i+1] = (byte) integer.parseint (Sarray[i+1].substring (2, 4), 16);
}
result = new String (MyByte, "utf-16");
}
}
Httpget.abort ();
return result;
} catch (Exception e) {
return null;
}
}


Please add: xiaoran-668

Multi-lingual translation using Baidu translation API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.