为了利用Google Translate API进行翻译,我们需要遵循以下步骤:
-
注册Google Cloud账户并启用Translate API
首先,我们需要在Google Cloud上注册一个账户并启用Translate API。在注册完成后,我们需要创建一个新的项目并在该项目下启用Translate API。创建项目的过程中需要生成一个API密钥,该密钥是后续代码使用Translate API时的重要凭证。 -
导入Google API客户端库
接下来,我们需要将Google的API客户端库(google-api-java-client)导入到我们的Java项目中。在Maven仓库中获取该库的最新版本即可。 -
准备翻译请求
我们需要创建一个Translate对象并调用其translate()方法进行翻译。该方法需要包含待翻译的文本和所需的目标语言做为参数。例如,我们需要将英文文本翻译为中文,调用translate()方法时需要传递源文本和“zh-CN”作为参数。
示例一:翻译单条文本
import java.util.Arrays;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.TranslateRequestInitializer;
import com.google.api.services.translate.TranslateScopes;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.collect.ImmutableList;
public class TranslateTextDemo {
public static void main(String[] args) throws Exception {
String text = "How are you?";
String targetLanguage = "zh-CN";
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
credentials = credentials.createScoped(ImmutableList.of(TranslateScopes.TRANSLATE));
Translate translateService = new Translate.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
new HttpCredentialsAdapter(credentials))
.setApplicationName("Translate-Service")
.setTranslateRequestInitializer(
new TranslateRequestInitializer("API Key"))
.build();
Translate.Translations.List list = translateService.new Translations().list(
Arrays.asList(text), targetLanguage);
list.setKey("API Key");
String translation = list.execute().getTranslations().get(0).getTranslatedText();
System.out.printf("Text: %s%nTranslation: %s%n", text, translation);
}
}
示例二:翻译多条文本
import java.util.Arrays;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.TranslateRequestInitializer;
import com.google.api.services.translate.TranslateScopes;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.collect.ImmutableList;
public class TranslateTextDemo {
public static void main(String[] args) throws Exception {
String[] texts = {"How are you?", "What is your name?", "Where are you from?"};
String targetLanguage = "zh-CN";
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
credentials = credentials.createScoped(ImmutableList.of(TranslateScopes.TRANSLATE));
Translate translateService = new Translate.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
new HttpCredentialsAdapter(credentials))
.setApplicationName("Translate-Service")
.setTranslateRequestInitializer(
new TranslateRequestInitializer("API Key"))
.build();
Translate.Translations.List list = translateService.new Translations().list(
Arrays.asList(texts), targetLanguage);
list.setKey("API Key");
String[] translations = list.execute().getTranslations()
.stream()
.map(Translate.TranslationsResource::getTranslatedText)
.toArray(String[]::new);
System.out.printf("Text: %s%nTranslations: %s%n", Arrays.toString(texts), Arrays.toString(translations));
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JAVA (Jsp)利用Google的Translate开发API的代码 - Python技术站