java 将中文翻译成英文
时间: 2023-10-11 18:13:34 浏览: 366
To translate Chinese to English in Java, you can use various libraries such as Google Translate API, Apache Tika, or OpenNMT. Here's an example using Google Translate API:
```java
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
public class Translator {
public static void main(String[] args) {
Translate translate = TranslateOptions.getDefaultInstance().getService();
Translation translation = translate.translate("你好", Translate.TranslateOption.sourceLanguage("zh-CN"), Translate.TranslateOption.targetLanguage("en"));
System.out.printf("Chinese: %s%nEnglish: %s%n", "你好", translation.getTranslatedText());
}
}
```
This code will output:
```
Chinese: 你好
English: Hello
```
阅读全文