Java Baidu translation API implementation for mutual translation between Chinese and Spanish
Introduction:
With the deepening development of globalization, the communication and communication between different languages The need for translation is increasing. This article will introduce how to use Java to develop a simple program and use Baidu Translation API to achieve mutual translation between Chinese and Spanish.
Add the following dependencies in the pom.xml file:
<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.10</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency> </dependencies>
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.json.JSONObject; public class TranslationClient { private static final String API_URL = "https://fanyi-api.baidu.com/api/trans/vip/translate"; private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; private static final String FROM = "zh"; private static final String TO = "es"; public static void main(String[] args) { String text = "你好世界"; try { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(API_URL); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); String body = String.format("q=%s&from=%s&to=%s&appid=%s&salt=%s&sign=%s", text, FROM, TO, APP_ID, salt, sign); httpPost.setEntity(new StringEntity(body)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String responseJson = EntityUtils.toString(entity); JSONObject jsonObject = new JSONObject(responseJson); String translation = jsonObject.getJSONArray("trans_result").getJSONObject(0).getString("dst"); System.out.println("Translation: " + translation); } catch (Exception e) { e.printStackTrace(); } } }
Please note that YOUR_ACCESS_TOKEN in the above code should be replaced with your own Access Token. Among them, API_URL is the request address of Baidu Translation API; FROM and TO represent the source language and target language respectively; text is the text to be translated.
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.json.JSONObject; public class TranslationClient { private static final String API_URL = "https://fanyi-api.baidu.com/api/trans/vip/translate"; private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; private static final String FROM = "es"; private static final String TO = "zh"; public static void main(String[] args) { String text = "Hola mundo"; try { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(API_URL); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); String body = String.format("q=%s&from=%s&to=%s&appid=%s&salt=%s&sign=%s", text, FROM, TO, APP_ID, salt, sign); httpPost.setEntity(new StringEntity(body)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String responseJson = EntityUtils.toString(entity); JSONObject jsonObject = new JSONObject(responseJson); String translation = jsonObject.getJSONArray("trans_result").getJSONObject(0).getString("dst"); System.out.println("Translation: " + translation); } catch (Exception e) { e.printStackTrace(); } } }
The above is the detailed content of Implementation of Java Baidu Translation API for Chinese and Spanish translation. For more information, please follow other related articles on the PHP Chinese website!