这篇文章上次修改于 1998 天前,可能其部分内容已经发生变化,如有疑问可询问作者。 >最近发现谷歌免费api有次数限制,所以只好调用他们收费api,还好他们免费试用,给了很多美金。 1.引入api的以来 ```xml com.google.cloud google-cloud-translate 1.76.0 com.google.guava guava 27.1-jre ``` 2.创建一个service的凭据,按这个链接操作:https://cloud.google.com/docs/authentication/getting-started 3.下载平局的json,然后java 代码 ```java @Component public class Translate2 { @Value(value = "classpath:my.json") private Resource googleJson; private TranslateOptions translateOptions; /** * 初始化消息配置 * * @throws IOException */ @PostConstruct public void initFireBase() throws IOException { List scops = new ArrayList<>(); scops.add("https://www.googleapis.com/auth/cloud-platform"); GoogleCredentials credentials = GoogleCredentials.fromStream(googleJson.getInputStream()); // .createScoped(scops); translateOptions = TranslateOptions.newBuilder().setCredentials(credentials).build(); } public String translateText(String text) throws IOException { if (StringUtils.isEmpty(text)) { text = "Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again. "; } return translateOptions.getService().translate("test", Translate.TranslateOption.sourceLanguage("en"), Translate.TranslateOption.targetLanguage("zh-CN")).getTranslatedText() ; } } ```
只有一条评论 (QwQ)
你好 有联系方式吗 项目也遇到了这个问题 请教下