一、安装Google Cloud SDK(http://blog.csdn.net/nicolelili1/article/details/68947164)
二、设置你的项目
1.登陆谷歌账号,没有就注册(https://accounts.google.com/SignUp)
https://accounts.google.com/signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin
2.在管理后台界面,选择或者新建一个新的项目(https://console.cloud.google.com/cloud-resource-manager?_ga=1.106468362.863623109.1502182438&pli=1)
点击“创建”
在列表中可以看到生成的这个项目
3.给项目允许账单,设置账号
点击左侧的下拉菜单,点击“结算”(如果没有绑定银行卡需要先绑定)
选择“管理结算账号”
选择“创建账号”
点击继续
点击“确认”(国家选择与信用卡相符的信息,比如写错了写成了美国,则会扣钱并由于信息与账号不相同可能会被封掉该账单,需要重新申诉)
创建成功
点击“我的项目”
选择刚创建的账号,并点击“设置账号”
四、启用云翻译API(Cloud Translation API)
1.打开网址https://console.cloud.google.com/flows/enableapi?apiid=translation.googleapis.com&_ga=1.1612088.863623109.1502182438,选择所要开启的项目
2.点击“转至API概览”
3.点击“ENABLE APIS AND SERVICES”
4.在搜索框输入“Google Cloud Translation API”进行搜索,查找到所需要的API
5.选择该API并且双击,在打开的界面中选择“启用”
启用后的界面
五、创建凭据
1、点击左侧的APIs&services-->凭据-->创建凭据-->API 密钥
六、初始化Google Cloud SDK
1.初始化输入命令gcloud init
2.选择重新初始化配置:1或重新创建一个新的配置
3.使用原来的账号:1或者重新登陆一个新的账号:2
4.选择所需要使用的项目
5、授权Google Cloud SDK
6、设置变量
计算机-->右键“属性”-->高级系统设置-->新建-->GOOGLE_API_KEY:密钥
七、配置
1.在build.gradle中添加依赖
compile 'com.google.cloud:google-cloud-translate:1.3.1'
private void googleTranslate() {
new Thread(new Runnable() {
@Override
public void run() {
//Translate translate = TranslateOptions.getDefaultInstance().getService();
Translate translate = TranslateOptions.newBuilder().setApiKey("AIzaSyAuPO3rl-CClveK6XulCapCITeSONanpVA").build().getService();
String sourceLanguage = "en";
String targetLanguage = "zh-CN";
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLanguage);
TranslateOption tgtLang = TranslateOption.targetLanguage(targetLanguage);
// Use translate `model` parameter with `base` and `nmt` options.
TranslateOption model = TranslateOption.model("nmt");
String sourceText = "Robinson Crusoe is a real hero. " +
"He almost has everything needed for becoming a successful man, " +
"such as his excellent creativity, great working capacity, courage, and persistence in overcoming obstacles. " +
"However, Robinson Crusoe is not a perfect man. " +
"He also has shortcomings. " +
"He was such a coward when he encountered a storm the first time. " +
"He sweared and repented yet ate his words and sailed on after the storm. " +
"By showing Robinson’s shortcomings, Daniel Defoe made Robinson a real person. " +
"After all, no one is perfect. " +
"Robinson Crusoe can not only lead the ambitious ones to success, but also guide average people to face up to life. " +
"This is the significance of the study of Robinson Crusoe";
Log.e("MainActivity", "googleTranslate.............................1");
Translation translation = translate.translate(sourceText, srcLang, tgtLang, model);
Log.e("MainActivity", "Source Text:.............................."+sourceLanguage+"..............."+sourceText);
Log.e("MainActivity", "TranslatedText.............................."+targetLanguage+"............"+translation.getTranslatedText());
}
}).start();
}
————————————————
版权声明:本文为CSDN博主「nicolelili1」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/nicolelili1/article/details/76973097