Youdao Fanyi
This is a Youdao Fanyi API wrapper written in Ruby.
Youdao Fanyi is a translation service provided by Youdao.
Pre Job
Before coding, you should go to Youdao Fanyi API for an API key.
Caution: Each key can request less than 1000 times per hour.
Example
# coding: UTF-8
# example.rb
require 'youdao-fanyi'
# Configure the key first
YoudaoFanyi::Config.key_from = "youdao-fanyi"
YoudaoFanyi::Config.key = 1629987369
# Then use the 4 methods provided.
to_be_translated = "要翻译的词句"
YoudaoFanyi.search_json(to_be_translated) # returns a JSON string
YoudaoFanyi.search_xml(to_be_translated) # returns an XML string
YoudaoFanyi.search_jsonp(to_be_translated) # returns a JSONP string
YoudaoFanyi.search_result_obj(to_be_translated) # returns a YoudaoFanyi::Result object
About the API
Youdao Fanyi API provides 3 types of data: xml, json and jsonp.
Here is an xml sample:
http://fanyi.youdao.com/fanyiapi.do?keyfrom=
<?xml version="1.0" encoding="UTF-8"?>
<youdao-fanyi>
<errorCode>0</errorCode>
<!-- 有道翻译 -->
<query><![CDATA[这里是有道翻译API]]></query>
<translation>
<paragraph><![CDATA[Here is the youdao translation API]]></paragraph>
</translation>
</youdao-fanyi>
Here is a json sample:
http://fanyi.youdao.com/fanyiapi.do?keyfrom=
{
"errorCode":0
"query":"翻译",
"translation":["translation"], // 有道翻译
"basic":{ // 有道词典-基本词典
"phonetic":"fān yì",
"explains":[
"translate",
"interpret"
]
},
"web":[ // 有道词典-网络释义
{
"key":"翻译",
"value":["translator","translation","translate","Interpreter"]
},
{...}
]
}
Here is a jsonp sample:
http://fanyi.youdao.com/fanyiapi.do?keyfrom=
show({
"errorCode":0
"query":"API",
"translation":["API"], // 有道翻译
"basic":{ // 有道词典-基本词典
"explains":[
"abbr. 应用程序界面(Application Program Interface);..."
]
},
"web":[ // 有道词典-网络释义
{
"key":"API",
"value":["应用程序接口(Application Programming Interface)","应用编程接口","应用程序编程接口","美国石油协会"]
},
{...}
]
});
Contributors
- ZHANG Hailong [https://github.com/zhhailon]