使用Google Translate API进行翻译 [英] Translating using Google Translate API
问题描述
我想使用以下代码作为将用户输入翻译为英语的程序的基础。我收到错误无法找到符号 - GoogleApi。有人可以帮我弄清楚该做什么吗?
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
公共类Main {
public static void main(String [] args)throws Exception {
//将HTTP引用设置为您的网站地址。
GoogleAPI.setHttpReferrer(/ *在此处输入您网站的网址* /);
//设置Google Translate API密钥
//请参阅:http://code.google.com/apis/language/translate/v2/getting_started.html
GoogleAPI .setKey(/ *在此处输入您的API密钥* /);
String translatedText = Translate.DEFAULT.execute(Bonjour le monde,Language.FRENCH,Language.ENGLISH);
System.out.println(translatedText);
}
}
A可以在
用于设置API密钥检查标题为设置API密钥的部分。
I want to use the following code as the basis for a program that translates user input to English. I am getting the error "cannot find symbol - GoogleApi." Can someone please help me figure out what to do?
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
public class Main {
public static void main(String[] args) throws Exception {
// Set the HTTP referrer to your website address.
GoogleAPI.setHttpReferrer(/* Enter the URL of your site here */);
// Set the Google Translate API key
// See: http://code.google.com/apis/language/translate/v2/getting_started.html
GoogleAPI.setKey(/* Enter your API key here */);
String translatedText = Translate.DEFAULT.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
System.out.println(translatedText);
}
}
A working code can be found here. It also has link to full codebase on github.
Things to keep in mind:
If you are behind proxy you need make jvm aware of this. One way of doing it is through a static initializer as below:
static { System.setProperty("http.proxyHost", HTTP_PROXY_HOST); System.setProperty("http.proxyPort", HTTP_PROXY_PORT); System.setProperty("https.proxyHost", HTTPS_PROXY_HOST); System.setProperty("https.proxyPort", HTTPS_PROXY_PORT); }
In Run configuration make your GOOGLE_API_KEY available by making it environment variable as below:
For setting up of API key Check section titled "Setting up an API Key".
这篇关于使用Google Translate API进行翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!