In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The purpose of this article is to share with you how to achieve the translation function in the development of Wechat. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Train of thought analysis
Similar to the idea of querying the weather in the previous article, the first step is to judge whether the message sent by the user contains the keyword "translation". If so, extract the content to be translated, and then call the translation API open on the network for relevant translation.
Translation API Analysis
There are many translators of API on the Internet, and you can choose according to your own needs. Here we choose Youdao Translation API and Baidu Translation API, which are widely used and have good translation functions. The following is an analysis of the relevant information about these two kinds of API.
3.1 youdao Translation API
3.1.1 API address: fanyi.youdao.com/openapi
Note: youdao provides the API interface, in the following test, the json data format returned is incorrect, to the Internet to consult information, the address can be correctly translated to fanyi.youdao.com/fanyiapi, this note.
3.1.2 apply for key
Fill in the relevant information as required, which will be used below, so please fill in carefully and truthfully.
After the application, API key and keyfrom will be generated below, which will be used when using API.
3.1.3 example of using API
3.1.4 data format
A. Xml format
Fanyi.youdao.com/openapi.do?keyfrom=orchid&key=1008797533&type=data&doctype=xml&version=1.1&q=, this is Youdao Translation API.
0
B. json format
Http://fanyi.youdao.com/openapi.do?keyfrom=orchid&key=1008797533&type=data&doctype=json&version=1.1&q= translation
{"errorCode": 0 "query": "translation", "translation": ["translation"], / / youdao translation "basic": {/ / youdao dictionary-basic dictionary "phonetic": "translate", "explains": ["translate", "interpret"]} "web": [/ / youdao dictionary-network interpretation {"key": "translation", "value": ["translator", "translation", "translate", "Interpreter"]}, {.}]}
3.2Baidu translates API
3.2.1 API address: openapi.baidu.com/public/2.0/bmt/translate
3.2.2 get api key
Developers register the authorized API key on Baidu connection platform. For more information, please see http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E7%BD%91%E7%AB%99%E6%8E%A5%E5%85%A5/%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97.
3.2.3 example of using API
3.2.4 data format
The data format of Baidu's translated API response is the standard JSON string corresponding to the UTF-8-encoded PHP array.
{"from": "zh", "to": "en", "trans_result": []}
Trans_result is an array, where each {} is a paragraph, and the structure is as follows:
Trans_result: [{}, {}, {}]
The result of the paragraph is an item in the trans_result array:
{"src": "," dst ":"}
The result of the paragraph states:
The form after json_decode:
{"from": "en", "to": "zh", "trans_result": [{"src": "today", "dst": "Today"}]}
IV. Keyword judgment and reading of the content to be translated
The format of the translation message is "translation + content to be translated", so first intercept the first two words to determine whether it is a "translation" keyword.
Use the php function mb_substr () to intercept. The usage of this function has been discussed in the previous article, so I won't repeat it here.
$str_trans = mb_substr ($keyword,0,2, "UTF-8")
Intercept from the beginning of the message, intercept two characters, and then determine whether it is a "translation" keyword.
$str_valid = mb_substr ($keyword,0,-2, "UTF-8")
Determine whether to enter only the word "translation". In this way, if there is no content to be translated, the message entered will be incorrect.
Next, the content to be translated is extracted:
$word = mb_substr ($keyword,2,220, "UTF-8")
Start with the third character at the beginning of the message, intercept 202 characters, and the intercepted content is the content to be translated.
Then the function is called for translation.
/ / call youdao dictionary $contentStr = $this- > youdaoDic ($word); / / call Baidu dictionary $contentStr = $this- > baiduDic ($word)
V. concrete realization
5.1 youdao Translation API
Data interface:
The text to be translated by http://fanyi.youdao.com/openapi.do?keyfrom=&key=&type=data&doctype=&version=1.1&q=
Replace the above keyfrom and key with the content of the above application, then select doctype, and then enter the text to be translated, you can call youdao Translation API for translation.
Youdao translation provides three data formats, and here we will only talk about two, namely xml and json.
5.1.1 xml format
The key code is as follows:
YoudaoDic (= "orchid"; = "YourApiKey"; = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='..'&key='..'&type=data&doctype=xml&version=1.1&q='. = (=-> =-> translation- > (= = 0 "cannot be translated effectively"
Description:
$xmlStyle = simplexml_load_file ($url_youdao); / / PHP function to load the XML document into the object.
$errorCode = $xmlStyle- > errorCode; / / get the error code
$paras = $xmlStyle- > translation- > paragraph; / / get the translated content
5.1.2 json format
The key code is as follows:
Public function youdaoDic ($word) {$keyfrom = "orchid"; / / contents of the website name filled in when applying for APIKEY $apikey = "YourApiKey"; / / APIKEY / / Youdao Translation-json applied from youdao in the format $url_youdao = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='.$keyfrom.'&key='.$apikey.'&type=data&doctype=json&version=1.1&q='.$word; $jsonStyle = file_get_contents ($url_youdao); $result = json_decode ($jsonStyle,true); $errorCode = $result ['errorCode']; $trans ='; if (isset ($errorCode)) {switch ($errorCode) {case 0: $trans = $result ['translation'] [' 0'] Break; case 20: $trans = 'the text to be translated is too long'; break; case 30: $trans = 'cannot be translated effectively'; break Case 40: $trans = 'unsupported language type'; break; case 50: $trans = 'invalid key'; break; default: $trans =' exception'; break }} return $trans;}
Description:
$jsonStyle = file_get_contents ($url_youdao); / / read the whole file into a string $result = json_decode ($jsonStyle,true); / / encode a pair of strings in JSON format $errorCode = $result ['errorCode']; / / get the error code $trans = $result [' translation'] ['0']; / / get the translation result
5.2Baidu translates API
Baidu translation API provides standard JSON strings corresponding to UTF-8-encoded PHP array, and provides four kinds of translation between Chinese-> English, Chinese-> Japanese, English-> Chinese-> Chinese-> Chinese, which is one more than Youdao translation.
The key code is as follows:
/ / Baidu translation public function baiduDic ($word,$from= "auto", $to= "auto") {/ / first perform urlencode processing on the text to be translated $word_code=urlencode ($word); / / registered API Key $appid= "YourApiKey" / / generate the URL GET address of the translated API $baidu_url = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".$appid."&q=".$word_code."&from=".$from."&to=".$to; $text=json_decode ($this- > language_text ($baidu_url)); $text= $text- > trans_result; return $text [0]-> dst } / / Baidu translation-get the printed content of the target URL public function language_text ($url) {if (! function_exists ('file_get_contents')) {$file_contents = file_get_contents ($url);} else {/ / initialize a cURL object $ch = curl_init () $timeout = 5; / / set the URL curl_setopt to be crawled ($ch, CURLOPT_URL, $url); / / set the cURL parameter that requires the result to be saved in a string or output to curl_setopt on the screen ($ch, CURLOPT_RETURNTRANSFER, 1) / / wait time before initiating a connection, if set to 0, wait for curl_setopt indefinitely ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); / / run cURL and request web page $file_contents = curl_exec ($ch); / / close URL request curl_close ($ch);} return $file_contents;}
Description:
There are two functions, baiduDic () and language_text ().
BaiduDic () function:
$word_code=urlencode ($word); / / first perform urlencode processing on the text to be translated $text=json_decode ($this- > language_text ($baidu_url)); / / call the language_text () function to get the printed content of the target URL, and then encode the string in JSON format $text= $text- > trans_result; / / to get the translation result array return $text [0]-> dst; / / take the dst result of the first array.
Language_text () function:
Determine whether the file_get_contents () function exists, and if so, use it to get the URL content; if not, use the cURL tool to get the URL content. See the code for details.
VI. Testing
Youdao Translation-xml format:
Youdao Translation-json format:
Baidu translator:
Thank you for reading! This is the end of the article on "how to achieve the translation function of Wechat development". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.