Use Baidu translation API for multilingual translation

Source: Internet
Author: User

Use Baidu translation API for multilingual translation

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: today}]}
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 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;
}
}

 

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.