This is a Youdao Fanyi API wrapper written in Ruby.
Youdao Fanyi is a translation service provided by Youdao.
Before coding, you should go to Youdao Fanyi API for an API key.
Caution: Each key can request less than 1000 times per hour.
# 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
Youdao Fanyi API provides 3 types of data: xml, json and jsonp.
Here is an xml sample:
http://fanyi.youdao.com/fanyiapi.do?keyfrom=&key=&type=data&doctype=xml&version=1.1&q=这里是有道翻译API
<?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=&key=&type=data&doctype=json&version=1.1&q=翻译
{
"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=&key=&type=data&doctype=jsonp&callback=show&version=1.1&q=API
show({
"errorCode":0
"query":"API",
"translation":["API"], // 有道翻译
"basic":{ // 有道词典-基本词典
"explains":[
"abbr. 应用程序界面(Application Program Interface);..."
]
},
"web":[ // 有道词典-网络释义
{
"key":"API",
"value":["应用程序接口(Application Programming Interface)","应用编程接口","应用程序编程接口","美国石油协会"]
},
{...}
]
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。