import com.google.cloud.translate.Translate;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
public class QuickstartSample {
static{
//国内访问需要加上代理
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "1080");
}
public static void main(String... args) throws Exception {
//服务账号json 配置的环境变量 为空则未成功配置
System.out.println( System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
// Instantiates a client
Translate translate = TranslateOptions.getDefaultInstance().getService();
TranslateOptions.getDefaultApiKey();
// The text to translate
String text = "Hello, world!";
// Translates some text into Russian
Translation translation =
translate.translate(
text,
Translate.TranslateOption.sourceLanguage("en"),
Translate.TranslateOption.targetLanguage("zh-CN"));
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translation.getTranslatedText());
}
}
Google 翻译
最新推荐文章于 2025-10-16 21:16:25 发布
本文介绍了一种使用Google Cloud翻译API进行文本翻译的方法,通过Java代码示例展示了如何将英语文本翻译成中文。
309

被折叠的 条评论
为什么被折叠?



