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、使用過程中出現錯誤: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)