微软翻译api的使用介绍和注意事项
google翻译api已经收费了,而 微软翻译 api 目前是免费的,支持几种不同的方式访问,如果感兴趣可以自己封装下协议处理。官方介绍:
http://msdn.microsoft.com/en-us/library/hh454950.aspx 。
这里介绍一下 java 下的使用。 Java 下使用微软翻译 api 可以直接使用一个开源的 sdk , http://code.google.com/p/microsoft-translator-java-api/ 。
一、首先去 http://code.google.com/p/microsoft-translator-java-api/ ,下载相关 jar 文件,这里有对微软翻译 api 的详细使用有作详细介绍。
二,去申请 key ,进入 http://www.bing.com/developers/createapp.aspx ,填写相关的你的应用信息就行了。就会有下面的图片中显示的 key ,中的 Application ID 就是 key 。
三、下面给个实例:
Java代码
/** * @Title: MicroTranslate.java * @Description: TODO( 用一句话描述该文件做什么 ) * @author zengzhaoshuai * @date 2012-2-13 下午 1:17:07 * @version V1.0 */ import com.memetix.mst.language.Language; import com.memetix.mst.translate.Translate; /** * @ClassName: MicroTranslate * @Description: TODO( 这里用一句话描述这个类的作用 ) * @author zengzhaoshuai * @date 2012-2-13 下午 1:17:07 * */ public class MicroTranslate { public static void main(String[] args) throws Exception { // Set the Microsoft Translator API Key - Get yours at http://www.bing.com/developers/createapp.aspx // Translate.setKey(/* Enter your API Key here */); Translate.setKey( " 自己申请的 key" ); String translatedText = Translate.execute( " 属性 " , Language.CHINESE_SIMPLIFIED, Language.ENGLISH); System.out.println(translatedText); } }
四、运行结果:Property
这里说下可能出现的问题:
1 、 android 下要用 microsoft-translator-java-api-0.6-jar-with-dependencies .jar ,否则运行时可能会报错 android java.lang.NoClassDefFoundError: org.json.simple.JSONValue 。
2 、使用过程中出现错误: T ranslateApiException: Cannot find an Azure Market Place Translator Subscription associated with the request credentials,则需要到网址上先订阅 translate api 的免费流量,操作如下:
M ake sure you login to azure market place click on data and select Microsoft Translator. Now click on 2000000 c/moth @ $0 update option. Now I think this error will go away. Try following link also. This is the link where I signed up for 2000000 c/month offer. I was facing the same problem but when I updated my account through following link, this error went away. ( 参考: http://social.msdn.microsoft.com/Forums/zh-CN/microsofttranslator/thread/1eeb0066-553f-4523-9a1b-0976e4205bb2 )
分类: Android , Java
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于微软翻译api的使用介绍和注意事项的详细内容...