Home > Java > Java Tutorial > body text

Utilize Java Baidu Translation API to achieve high-quality translation between multiple languages

WBOY
Release: 2023-08-07 19:09:03
Original
1127 people have browsed it

Use Java Baidu Translation API to achieve high-quality translation between multiple languages

In today's era of globalization, cross-language communication has become more and more important. In order to meet the needs of cultural and economic exchanges between different countries and regions, translation technology has become an indispensable technology. Baidu Translation API provides high-quality machine translation services, and accessing it using the Java programming language provides developers with convenient and fast translation tools.

Baidu Translation API is a machine translation interface based on HTTP/HTTPS protocol that supports multiple programming languages. It provides real-time translation between multiple languages, including but not limited to English, Chinese, French, German, Japanese, etc. By writing code in Java, we can easily call API interfaces to achieve high-quality translation between different languages.

First of all, we need to apply for a translation API account on Baidu Open Platform and obtain the corresponding API Key and Secret Key. This information will be used for subsequent API access.

Next, we can use the HTTP request library in Java, such as Apache HttpClient to send HTTP requests. Here is a simple sample code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class TranslationDemo {
    public static void main(String[] args) {
        String apiKey = "your_api_key";
        String secretKey = "your_secret_key";
        
        try {
            // 要翻译的文本
            String text = "Hello, world!";
            String textEncoded = URLEncoder.encode(text, "UTF-8");
            
            // 构建请求URL
            String url = "https://fanyi-api.baidu.com/api/trans/vip/translate";
            url += "?q=" + textEncoded;
            url += "&from=en&to=zh";
            url += "&appid=" + apiKey;
           
            // 发送HTTP GET请求
            URL requestUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();

            // 获取翻译结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            
            // 处理翻译结果
            // 这里的response为JSON格式的字符串,我们可以使用JSON解析库进行处理
            
            System.out.println(response.toString());
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In this example, we first URL-encode the text to be translated, and then build the request URL for the translation API. We use the universal translation interface of Baidu Translation API. Pass the text to be translated to the q parameter, and the source and target languages ​​to the from parameter and the to parameter respectively. We also need to pass the API Key we applied for before and verify it through the appid parameter.

Next, we use an HTTP GET request to send the request URL to the Baidu Translation API and save the returned translation result in a string. Finally, we can use the JSON parsing library to process the returned string and extract the translation results.

The above is a simple example of using the Java Baidu Translation API to achieve high-quality translation between multiple languages. By calling the API interface, we can easily perform real-time translation between different languages ​​and implement more interesting functions in development. The development of translation technology makes our world closer and more colorful.

The above is the detailed content of Utilize Java Baidu Translation API to achieve high-quality translation between multiple languages. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!