»network technique»[2018/4] The latest available PHP online call Google online translation API code
Calling Google's online translation API online can realize automatic translation of text in some projects
the front
When it comes to translation, Google Translate is the first choice. I have to say that Google has done a good job in translation. Today Cloud Cloud needs to use translation, and several domestic translations are not very satisfactory, but Google has completed the task very well. What is said here is not to use it in web pages, but to use it as an api in code. I have tried Baidu and Youdao translation apis,None of them are very good. I can only choose Google's.
demand
Yunluo is currently working on the I love Chrome plugin website, which needs to translate the plugin summary and description. If it is all manual translation, it will be too troublesome, so I pursue to automatically translate articles into Chinese when they are published. Baidu found that Google's translation api needs a lot, but many of the methods of articles are invalid or troublesome. Yunluo is lazy, of course,Therefore, after my own research, it is finally possible to achieve online translation without TKK, which at least saves the step of manual translation of English instructions.
code
Because Cloudfall is used in WordPress, it only has a PHP version. Of course, it is capable of understanding at a glance, and the code is cobbled together. If there is anything that can be improved, please point out.
<?php$longtext = 'Joymems, Get nostalgic. See your own photos on every new Chrome tab.Spice up your new tabs with YOUR photos. Every new tab will show your memories from your photo albums. Simple, intuitive and personal.See the people you love, the moments you care about, the things that inspire you. Your photos, your story!';function gtranslate($text){$entext = urlencode($text);$url = 'http://translate.google.cn/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl=auto&tl=zh-CN&q='.$entext;set_time_limit(0);$ch = curl_init();curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch, CURLOPT_MAXREDIRS,20);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 40);curl_setopt($ch, CURLOPT_URL, $url);$result = curl_exec($ch);curl_close($ch);$result = json_decode($result);if(!empty($result)){foreach($result[0] as $k){$v[] = $k[0];}return implode(" ", $v);}}echo gtranslate($longtext);?>
When the amount of text is large, there may be blank
Put the above code into your own server to run, and you can see the translation results.,The result is:
Joymems, nostalgia.View your own photos on each new Chrome tab.\N Use your photos to add color to your new label.Each new tab will show your album.Simple, intuitive and personal.\N n See the people you love, the moments you care about, and the things that inspire you.Your photo, your story!\N n It is private and free.
Translation only in English
Originally, Yunluo had realized the function of translation, but later found that no matter what language it was, it would be unnecessary if it was in Chinese. So we made a judgment on the process of translation. Originally, we wanted to use regular translation, but we thought it was inefficient, so we used another method, which is also very simple. It is to compare the length of the text. Use the following code to determine whether the text is in English
$text = 'i go to school';$v = strlen($text);$k = mb_strlen($text,'utf-8');
Let's see how it works
We can do this in practical applications
$text='i go to school ';$v = strlen($text);$k = mb_strlen($text,'utf-8');if($v = $k){$result = gtranslate($text);}echo $result;
last
Finally, I will show another website that I have been working on recently. I love Chrome Plug in Network, which is mainly used to download plug-in services for Chrome browsers. WordPress website, with the theme of Git, has customized and developed some functions on Git. I love Chrome Plug in Network