[2018/4] The latest available PHP online call Google online translation API code

» 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

【2018/4】最新可用PHP在线调用谷歌在线翻译API代码-极客公园

the front

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
【2018/4】最新可用PHP在线调用谷歌在线翻译API代码-极客公园

【2018/4】最新可用PHP在线调用谷歌在线翻译API代码-极客公园

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

--End--

reply dev Cancel Reply

Your email address will not be disclosed. Required items have been used * tagging

7 Replies to "[2018/4] The latest available PHP online call Google online translation API code"