下面是一个工作示例。
你需要为你的应用生成自己的应用密钥(从这里开始),因为翻译 API 不再公开可用。
有关传递到Translate.Builder()中的选项,请参阅此处。
import java.util.Arrays;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.model.TranslationsListResponse;
import com.google.api.services.translate.model.TranslationsResource;
public class TranslateMe {
public static void main(String[] args) {
try {
// See comments on
// https://developers.google.com/resources/api-libraries/documentation/translate/v2/java/latest/
// on options to set
Translate t = new Translate.Builder(
com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport()
, com.google.api.client.json.gson.GsonFactory.getDefaultInstance(), null)
//Need to update this to your App-Name
.setApplicationName("Stackoverflow-Example")
.build();
Translate.Translations.List list = t.new Translations().list(
Arrays.asList(
//Pass in list of strings to be translated
"Hello World",
"How to use Google Translate from Java"),
//Target language
"ES");
//Set your API-Key from https://console.developers.google.com/
list.setKey("you-need-your-own-api-key");
TranslationsListResponse response = list.execute();
for(TranslationsResource tr : response.getTranslations()) {
System.out.println(tr.getTranslatedText());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}