Use Baidu translation API for multilingual translation and Baidu translation api for implementation
Supported languages:
China, Britain, China, Japan, China, Korea, China, France, China, China, West, China, Thailand, Thailand, China, China, Canada, China, Russia, China, Japan, Britain, Japan, Britain, Britain, Thailand, and Britain english, Western, Portuguese, Portuguese
Frequency limit:
Provided by common developers1000 times/hourRestrictions: supports resizing;
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, 11 languages are supported, as shown below:
ChineseZhEnglishEn
JapaneseJpKoreanKor
SpanishSpaFrenchFra
ThaiThArabicAra
RussianRuPortuguesePt
CantoneseYueClassical ChineseWyw
VernacularZhAutomatic DetectionAuto
Steps:
Step1. apply for ApiKey,Http://developer.baidu.com/console#app/project
Step2. form a Get request
Step3. get the response
Step 4. parse the response (key)
Step 5. result output
Key code (C #):
Public string getTraslation (string src)
{
// Send the request
HttpWebRequest myReq = (HttpWebRequest) WebRequest. Create ("http://openapi.baidu.com/public/2.0/bmt/translate? Client_id = "+ client_id +" & q = "+ src +" & from = "+ from +" & to = "+ );
// Get the response
String res = string. Empty;
Try
{
// Obtain the response stream
HttpWebResponse response = (HttpWebResponse) myReq. GetResponse ();
StreamReader reader = new StreamReader (response. GetResponseStream (), Encoding. UTF8 );
Res = reader. ReadToEnd ();
Reader. Close ();
Response. Close ();
// Regular Expression
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 in front of a small client
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 = "+ );
HttpResponse = httpClient.exe cute (httpGet );
HttpEntity = httpResponse. getEntity ();
String content = EntityUtils. toString (httpEntity, "UTF-8 ");
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