一、安装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();
}
运行后的结果:
原来的语言
Source Text:..............................en...............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
翻译后的语言
TranslatedText..............................zh-CN............罗宾逊漂流记是真正的英雄。他几乎拥有成为一个成功的人所需要的一切,如他的出色的创造力,伟大的工作能力,勇气和坚持克服障碍。然而,罗宾逊漂流记不是一个完美的人。他也有缺点。当他第一次遇到风暴时,他是个懦夫。他发誓和悔改,吃了他的话,在风暴后航行。通过显示罗宾逊的缺点,丹尼尔·迪福让罗宾逊成为一个真正的人物。毕竟,没有人是完美的。罗宾逊漂流记不仅可以引导雄心勃勃的成功,也可以引导普通人面对生活。这是对鲁滨逊漂流记的研究的意义
注:1.参考资料:https://cloud.google.com/translate/docs/getting-started
https://cloud.google.com/docs/authentication/getting-started
http://blog.csdn.net/wangbailin2009/article/details/72742246
https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-translate
2.jar下载的地址:https://jar-download.com/?detail_search=g%3A%22com.google.cloud%22&g=com.google.cloud&p=3
遇到的问题
3.Error:Failed to resolve: com.google.cloud:google-cloud-translate:0.20.1-alpha
按照官网缩写的添加如下的依赖后出现上面的问题
解决办法:
将 compile group: 'com.google.cloud', name: 'google-cloud-translate', version: '0.20.1-alpha' 替换为 compile 'com.google.cloud:google-cloud-translate:1.3.1'
4.Execution failed for task ':app:transformClassesWithDexForOffcialSiteDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
解决办法:
在build.gradle中允许multiDex
android {
defaultConfig {
...
// Enabling multidex support.
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
5、
Execution failed for task ':app:transformResourcesWithMergeJavaResForOffcialSiteDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK project.properties
File1: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-translate\1.3.1\ccef30bae3b4358c83d671cc8731bbbde4ed1d17\google-cloud-translate-1.3.1.jar
File2: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-core\1.3.1\6688ecc0b0527ddd08fad76adc5419ba40b68599\google-cloud-core-1.3.1.jar
Duplicate files copied in APK project.properties
File1: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-translate\1.3.1\ccef30bae3b4358c83d671cc8731bbbde4ed1d17\google-cloud-translate-1.3.1.jar
File2: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-core\1.3.1\6688ecc0b0527ddd08fad76adc5419ba40b68599\google-cloud-core-1.3.1.jar
解决办法:配置不添加这个包选项
android {
packagingOptions {
exclude 'project.properties'
}
}
6.
* What went wrong:
Execution failed for task ':app:transformResourcesWithMergeJavaResForOffcialSiteDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/INDEX.LIST
File1: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-core-http\1.3.1\13ff41068c2db0e43cf3ed96e24746fabbcbee21\google-cloud-core-http-1.3.1.jar
File2: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-translate\1.3.1\ccef30bae3b4358c83d671cc8731bbbde4ed1d17\google-cloud-translate-1.3.1.jar
File3: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-core\1.3.1\6688ecc0b0527ddd08fad76adc5419ba40b68599\google-cloud-core-1.3.1.jar
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 2.86 secs
Duplicate files copied in APK META-INF/INDEX.LIST
File1: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-core-http\1.3.1\13ff41068c2db0e43cf3ed96e24746fabbcbee21\google-cloud-core-http-1.3.1.jar
File2: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-translate\1.3.1\ccef30bae3b4358c83d671cc8731bbbde4ed1d17\google-cloud-translate-1.3.1.jar
File3: C:\Users\zjhl\.gradle\caches\modules-2\files-2.1\com.google.cloud\google-cloud-core\1.3.1\6688ecc0b0527ddd08fad76adc5419ba40b68599\google-cloud-core-1.3.1.jar
解决办法:添加不包括INDEX.LIST
packagingOptions {
exclude 'project.properties'
exclude 'META-INF/INDEX.LIST'
}
6.com.google.cloud.translate.TranslateException: The request is missing a valid API key.
原因:本来TranslateOptions.getDefaultInstance().getService()会默认调用在电脑上配置的GOOGLE_API_KEY的值的,但不知道哪步出错了,没配置上,可以使用下面的方法动态设置API Key
解决办法:
Translate translate = TranslateOptions.getDefaultInstance().getService();
变成动态从代码中设置app key
Translate translate = TranslateOptions.newBuilder().setApiKey("AIzaSyAuPO3rl-CClwwwkeXulCapCITeSONanpVA").build().getService();