调用有道智云API,自动翻译WORDPRESS标题为英文

  • A+
所属分类:PHP开发
摘要

调用有道智云API,自动翻译WORDPRESS标题为英文。

调用有道智云API,自动翻译WORDPRESS标题为英文

wordpress

调用有道智云API,自动翻译WORDPRESS标题为英文。

代码:

<?php
/*
Plugin Name:zimeng_slug_translator
Plugin URI: https://chenxuehu.com/
Description: 调用有道翻译API,自动将中文标题翻译成英语.
Version: 1.0.0
Author: Xuehu
Author URI:https://chenxuehu.com/
License: GPL
*/

define("CURL_TIMEOUT",   20); 
define("URL",            "http://openapi.youdao.com/api"); 
define("APP_KEY",         ""); 
define("SEC_KEY",        "");

/**
 * 有道API翻译入口
 */
if( !function_exists("zimeng_youdao_translate")){
    function zimeng_youdao_translate($query, $from, $to)
    {
        $args = array(
            'q' => $query,
            'appKey' => APP_KEY,
            'salt' => rand(10000,99999),
            'from' => $from,
            'to' => $to,

        );
        $args['sign'] = md5(APP_KEY.$query.$args['salt'].SEC_KEY);
        $ret = zimeng_youdao_call(URL, $args);
        $ret = json_decode($ret, true);
        return $ret['translation'][0]; 
    }
}

if( !function_exists("zimeng_youdao_call")){
    function zimeng_youdao_call($url, $args=null, $method="post", $testflag = 0, $timeout = CURL_TIMEOUT, $headers=array())
    {
        $ret = false;
        $i = 0; 
        while($ret === false) 
        {
            if($i > 1)
                break;
            if($i > 0) 
            {
                sleep(1);
            }
            $ret = zimeng_youdao_callOnce($url, $args, $method, false, $timeout, $headers);
            $i++;
        }
        return $ret;
    }
}

if( !function_exists("zimeng_youdao_callOnce")){
    function zimeng_youdao_callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = CURL_TIMEOUT, $headers=array())
    {
        $ch = curl_init();
        if($method == "post") 
        {
            $data = zimeng_youdao_convert($args);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
        }
        else 
        {
            $data = zimeng_youdao_convert($args);
            if($data) 
            {
                if(stripos($url, "?") > 0) 
                {
                    $url .= "&$data";
                }
                else 
                {
                    $url .= "?$data";
                }
            }
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if(!empty($headers)) 
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if($withCookie)
        {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
        }
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }
}

if( !function_exists("zimeng_youdao_convert")){
    function zimeng_youdao_convert(&$args)
    {
        $data = '';
        if (is_array($args))
        {
            foreach ($args as $key=>$val)
            {
                if (is_array($val))
                {
                    foreach ($val as $k=>$v)
                    {
                        $data .= $key.'['.$k.']='.rawurlencode($v).'&';
                    }
                }
                else
                {
                    $data .="$key=".rawurlencode($val)."&";
                }
            }
            return trim($data, "&");
        }
        return $args;
    }
}

if( !function_exists("ZimengSlugTrans")){
    function ZimengSlugTrans($postID){
    global $wpdb;
    
    $tableposts = $wpdb->posts ;
    $sql = "SELECT post_title,post_name FROM $tableposts WHERE ID=$postID";
    $res = $wpdb->get_results($sql);    
    $post_title = $res[0]->post_title;
    $tran_title = zimeng_youdao_translate($post_title,"zh-CHS","EN");
    $slug = $tran_title;
    if(function_exists("sanitize_title") ) {
        if( sanitize_title( $res[0]->post_title ) != $res[0]->post_name  ){
            if( !substr_count($path, '%') ) 
                return true;
        }
        $slug = sanitize_title( $slug);
        if( strlen($slug) < 2 ) return true;
    }
        $sql ="UPDATE ".$tableposts." SET `post_name` = '".$slug."' WHERE ID =$postID;";        
        $res = $wpdb->query($sql);
            
    }   
}
 
add_action('publish_post', 'ZimengSlugTrans');
add_action('edit_post', 'ZimengSlugTrans');

 

做成了插件,但是没有做后台界面,要使用需要手动的修改文件,设置以下的信息为您的申请的信息:

define("APP_KEY",         ""); 
define("SEC_KEY",        "");

该翻译的API是付费的,酌情使用。

插件版本下载,修改信息后,后台启用插件即可。

 

 

文件下载 调用有道智云API,自动翻译WORDPRESS标题为英文 WORDPRESS
下载密码:发表评论并刷新可见!
下载地址
  • 我的微信
  • 加我请备注缘由
  • weinxin
  • 我的微信公众号
  • 轻松随便关注
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

关闭震动