[2018 / 4] the latest PHP online translation api code can be called online

» network technique » [2018 / 4] the latest PHP online translation api code can be called online

Online call Google online translation API, you can achieve automatic text translation in some projects

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

the front

demand

Yunluo has been working on the chrome plug-in website recently. It needs to translate the summary and description of the plug-in. If it's all manual translation, it's too much work. So I pursue to automatically translate the article into Chinese when I publish it. Baidu found that Google translation API needs a lot of things, but many methods of articles are invalid or troublesome. Of course, Yunluo is relatively lazy, Therefore, under my own research, it is finally possible to realize online translation without TKK, which can at least save the steps of manual translation of English instructions.

code

Because cloud cloud is used in WordPress, it only has a PHP version. Of course, I have the ability to understand at a glance. The code is patchy. If there is any improvement, 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 relatively large, there may be a blank situation

Put the above code into your own server and run it to see the translation result., The results were as follows:

 Joymems, nostalgia. View your own photos on each new chrome tab\ N add color to your new label with your photos. Each new tab will show your album. Simple, intuitive and personal\ See the people you love, the moments you care about, the things that motivate you. Your picture, your story\ It's private and free.

Translation in English only

Originally, Yunluo has already realized the function of translation, but later found that no matter what language it is, it will be unnecessary if it is in Chinese. Therefore, to make a judgment on the process of translation, I originally wanted to use regular, but I felt that the efficiency was not very good, so I used another method, which is actually very simple, that is to compare the length of the text.
Use the following code to determine whether the text is English

 $text = 'i go to school'; $v = strlen($text); $k = mb_ strlen($text,'utf-8');

Let's actually look at the operation
【2018/4】最新可用PHP在线调用谷歌在线翻译API代码-极客公园

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

We can do this in practice

 $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'll show another website I've been working on recently. I love chrome plug-in website. It's mainly used to download plug-ins for chrome browsers. The theme of WordPress website is GIT. I've customized and developed some functions on GIT.
I love chrome plug-in

--Over--

Comment

Your email address will not be disclosed. Required item used * tagging

7 replies to "[2018 / 4] the latest PHP online translation api code can be called online"