Home >Java >javaTutorial >Advanced translation between Chinese and Latvian via Java Baidu Translation API
Advanced translation between Chinese and Latvian through Java Baidu Translation API
In the Internet age, cross-language communication has become a norm. Whether in international travel, business exchanges or study exchanges, the application of translation tools has become very common. Among them, Baidu Translation API is a very excellent translation tool that can provide accurate and fast translation services.
In this article, we will introduce how to use the Java Baidu Translation API to achieve advanced translation between Chinese and Latvian. We will use the HTTP interface of Baidu Translation API to send the request and parse the returned translation results through Java code. Next, we will introduce the implementation process step by step.
First of all, we need to obtain a developer account for Baidu Translation API in order to obtain API calling permission. After the registration is completed, we can get an APP ID and a key, which will be used in subsequent operations.
Next, we need to build a Java project and add the corresponding dependency packages. We can use Maven to manage these dependency packages. In the project's pom.xml file, add the following dependencies:
<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> </dependencies>
Next, we need to create a TranslateUtil class to implement the translation function. In this class, we need to write the following code:
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import com.google.gson.Gson; import com.google.gson.JsonObject; public class TranslateUtil { private static final String APP_ID = "your_app_id"; private static final String SECRET_KEY = "your_secret_key"; public static String translate(String query, String from, String to) { try { String salt = String.valueOf(System.currentTimeMillis()); String sign = MD5(APP_ID + query + salt + SECRET_KEY); // 组装请求URL String url = "http://api.fanyi.baidu.com/api/trans/vip/translate"; url += "?q=" + URLEncoder.encode(query, "UTF-8"); url += "&from=" + from; url += "&to=" + to; url += "&appid=" + APP_ID; url += "&salt=" + salt; url += "&sign=" + sign; // 发送HTTP请求 HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestMethod("GET"); // 获取翻译结果 InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); is.close(); // 解析JSON结果 Gson gson = new Gson(); JsonObject obj = gson.fromJson(sb.toString(), JsonObject.class); JsonObject transResult = obj.getAsJsonArray("trans_result").get(0).getAsJsonObject(); String result = transResult.get("dst").getAsString(); return result; } catch (Exception e) { e.printStackTrace(); return null; } } private static String MD5(String s) { // ... } }
In the above code, we first define the APP ID and SECRET KEY in the TranslateUtil class, and then write a translate method, which is used to receive the required Translated text, source language, and target language, and returns the translation results.
In the translate method, we first generated a random salt and a signature sign. Then, we concatenate these parameters and translated content into a request URL according to the API requirements, and send an HTTP request. Next, we get the translated results by parsing the returned JSON results.
Finally, we can call the TranslateUtil class through the following code and print out the translation results:
public class Main { public static void main(String[] args) { String query = "你好"; String from = "zh"; String to = "lv"; String result = TranslateUtil.translate(query, from, to); System.out.println(result); } }
In the above code, we passed in a Chinese string "Hello", Specify the source language as "zh" (Chinese) and the target language as "lv" (Latvian). Finally, we get the translation results by calling the translate method of TranslateUtil and print the results.
Through the above steps, we can achieve advanced translation between Chinese and Latvian. With the help of the Java Baidu Translation API, we can easily implement multi-language translation functions in our own applications to provide users with better services and experiences.
The above is the detailed content of Advanced translation between Chinese and Latvian via Java Baidu Translation API. For more information, please follow other related articles on the PHP Chinese website!