微软翻译api的使用介绍和注意事项

 google翻译api已经收费了,而微软翻译api目前是免费的,支持几种不同的方式访问,如果感兴趣可以自己封装下协议处理。官方介绍:

http://msdn.microsoft.com/en-us/library/hh454950.aspx

这里介绍一下java下的使用。Java下使用微软翻译api可以直接使用一个开源的sdkhttp://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代码  

收藏代码

      1.   /**    
      1. * @Title: MicroTranslate.java  
      1. * @Description: TODO(用一句话描述该文件做什么 
      1. * @author zengzhaoshuai    
      1. * @date 2012-2-13 下午1:17:07  
      1. * @version V1.0    
      1. */  
      1.   
      1. import com.memetix.mst.language.Language;  
      1. import com.memetix.mst.translate.Translate;  
      1.   
      1. /**  
      1.  * @ClassName: MicroTranslate  
      1.  * @Description: TODO(这里用一句话描述这个类的作用 
      1.  * @author zengzhaoshuai  
      2.  * @date 2012-2-13 下午1:17:07  
      1.  *   
      1.  */  
      1. public class MicroTranslate {  
      1.     public static void main(String[] args) throws Exception {  
      1.         // Set the Microsoft Translator API Key - Get yours at http://www.bing.com/developers/createapp.aspx  
      1.        // Translate.setKey(/* Enter your API Key here */);  
      1.         Translate.setKey("自己申请的key");  
      1.   
      1.         String translatedText = Translate.execute("属性", Language.CHINESE_SIMPLIFIED, Language.ENGLISH);  
      1.   
      1.         System.out.println(translatedText);  
      1.     }  
      1.   
      1. }  

 

四、运行结果:Property 

 

这里说下可能出现的问题:

1android下要用microsoft-translator-java-api-0.6-jar-with-dependencies.jar,否则运行时可能会报错android java.lang.NoClassDefFoundError: org.json.simple.JSONValue

2、使用过程中出现错误:TranslateApiException: Cannot find an Azure Market Place Translator Subscription associated with the request credentials,则需要到网址上先订阅translate api的免费流量,操作如下:

Make 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

posted @ 2012-05-24 14:22  brainy  阅读(14227)  评论(10编辑  收藏  举报