qTranslate 和 AJAX 的 Wordpress 问题

Posted

技术标签:

【中文标题】qTranslate 和 AJAX 的 Wordpress 问题【英文标题】:Wordpress problems with qTranslate and AJAX 【发布时间】:2013-12-09 18:13:31 【问题描述】:

我正在使用带有 qTranslate 和自定义主题的 wordpress。该主题带来了自动完成搜索。我的问题是帖子/页面标题没有翻译。我检查了 ***/google,AJAX 请求和 qTranslate 似乎有问题。这就是为什么我在我的 php 文件的 url 参数中添加了以下内容:

?lang='.qtrans_getLanguage()

我检查了控制台,并且参数已正确(针对每种语言)传递给 javascript 文件。我现在需要的是读取这个 url 参数并将其返回给我的 php 文件(我测试了 js 文件,在使用 $.getJSON 时将 lang=en 添加到 url 并且它有效,但我需要传递的语言)。

PHP

<?php

    add_action( 'init', 'myprefix_autocomplete_init' );  
    function myprefix_autocomplete_init()   
        // Register our jQuery UI style and our custom javascript file  
        wp_register_script( 'my_acsearch', get_template_directory_uri() . '/nectar/assets/functions/ajax-search/wpss-search-suggest.js?lang='.qtrans_getLanguage(), array('jquery','jquery-ui-autocomplete'),null,true);  
        wp_localize_script( 'my_acsearch', 'MyAcSearch', array('url' => admin_url( 'admin-ajax.php' ))); 

        // Function to fire whenever search form is displayed  
        add_action( 'get_search_form', 'myprefix_autocomplete_search_form' );  

        // Functions to deal with the AJAX request - one for logged in users, the other for non-logged in users.  
        add_action( 'wp_ajax_myprefix_autocompletesearch', 'myprefix_autocomplete_suggestions' );  
        add_action( 'wp_ajax_nopriv_myprefix_autocompletesearch', 'myprefix_autocomplete_suggestions' );  
      

    function myprefix_autocomplete_search_form()  
        wp_enqueue_script( 'my_acsearch' );  
      

    add_action( 'wp_ajax_action', 'my_hooked_function' );  
    add_action( 'wp_ajax_nopriv_action', 'my_hooked_function' );  

    function myprefix_autocomplete_suggestions()  

        $search_term = $_REQUEST['term'];
        $search_term = apply_filters('get_search_query', $search_term);

        $search_array = array(
            's'=> $search_term, 
            'showposts'   => 6,
            'post_type' => 'any', 
            'post_status' => 'publish', 
            'post_password' => '',
            'suppress_filters' => true
        );

        $query = http_build_query($search_array);

        $posts = get_posts( $query );

        // Initialise suggestions array  
        $suggestions=array();  

        global $post;  

        foreach ($posts as $post): setup_postdata($post);  

            $suggestion['lang'] = $_GET['lang'];
            $suggestion['label'] = esc_html(qtrans_use(qtrans_getLanguage(), $post->post_title, true)); 
            $suggestion['link'] = get_permalink();  
            $suggestion['image'] = (has_post_thumbnail( $post->ID )) ? get_the_post_thumbnail($post->ID, 'thumbnail', array('title' => '')) : '<i class="icon-salient-pencil"></i>' ; 

            if(get_post_type($post->ID) == 'post')

                $suggestion['post_type'] = __('Blog Post',NECTAR_THEME_NAME); 

             else if(get_post_type($post->ID) == 'page')

                $suggestion['post_type'] = __('Page',NECTAR_THEME_NAME); 

             else if(get_post_type($post->ID) == 'portfolio')

                $suggestion['post_type'] = __('Portfolio Item',NECTAR_THEME_NAME); 

                //show custom thumbnail if in use
                $custom_thumbnail = get_post_meta($post->ID, '_nectar_portfolio_custom_thumbnail', true); 
                if(!empty($custom_thumbnail) )
                    $attachment_id = pn_get_attachment_id_from_url($custom_thumbnail);
                    $suggestion['image'] = wp_get_attachment_image($attachment_id,'portfolio-widget');
                

             else if(get_post_type($post->ID) == 'product')

                $suggestion['post_type'] = __('Product',NECTAR_THEME_NAME); 
            

            // Add suggestion to suggestions array  
            $suggestions[]= $suggestion;  
        endforeach;  

        // JSON encode and echo  
        $response = $_GET["callback"] . "(" . json_encode($suggestions) . ")";  
        echo $response;  

        // Don't forget to exit!  
        exit;  
       

?>

JS

jQuery(document).ready(function ($)  

    var acs_action = 'myprefix_autocompletesearch';  
    $("#s").autocomplete( 
        delay: 50,
        position: of: "#search-outer #search .container" ,
        appendTo: $("#search-box"), 
        source: function(req, response)  
            $.getJSON(MyAcSearch.url+'?callback=?&action='+acs_action, req, response);  
        ,  
        select: function(event, ui)   
            window.location.href=ui.item.link;  
        ,  
        minLength: 2,  
    ).data( "ui-autocomplete" )._renderItem = function( ul, item ) 
        return $( "<li>" )
        .append( "<a>" + item.image + "<span class='title'>" + item.label + "</span><br/><span class='desc'>" + item.post_type + "</span> </a>" )
        .appendTo( ul );
    ;  

); 

希望有人能帮帮我

【问题讨论】:

【参考方案1】:

即使这不是一个好的解决方案,我还是想出了这个:

var lang;

$('script').each(function(i, e) 
    var scriptName = $(e).attr('src');

    if(typeof scriptName != 'undefined') 
        if(scriptName.indexOf('wpss-search-suggest') != -1) 
            lang = scriptName.substr(scriptName.lastIndexOf('=')+1, scriptName.length-1);
        
    
);  

$.getJSON(MyAcSearch.url+'?callback=?&lang='+lang+'&action='+acs_action, req, response); 

【讨论】:

【参考方案2】:

我找到了一个更简单的解决方法,但条件 php 是必要的(使用条件将脚本硬编码到 php 文件中):

代替:

window.location.href=ui.item.link;

你可以使用:

var lang = '<?php echo qtrans_getLanguage(); ?>';
window.location.href=ui.item.link+'?lang='+lang;

所以脚本是(我还包括另一个检查 qTranslate 是否被激活的条件):

  //
  // Autocomplete Searchform
  var acs_action = 'myprefix_autocompletesearch';
  jQuery("#s").autocomplete(
      delay: 50,
      position: of: "#search-outer #search .container" ,
      appendTo: $("#search-box"), 
      source: function(req, response)
          jQuery.getJSON(MyAcSearch.url+'?callback=?&action='+acs_action, req, response);
      ,
      select: function(event, ui) 
<?php if (function_exists('qtrans_getLanguage')) ?>
          var lang = '<?php echo qtrans_getLanguage(); ?>';
          window.location.href=ui.item.link+'?lang='+lang;
<?php else ?>
          window.location.href=ui.item.link;
<?php  ?>
      ,
      minLength: 2,
    ).data( "ui-autocomplete" )._renderItem = function( ul, item ) 
        return jQuery( "<li>" )
        .append( "<a>" + item.image + "<span class='title'>" + item.label + "</span></a>" )
        .appendTo( ul );
    ;

希望对你有帮助!

【讨论】:

以上是关于qTranslate 和 AJAX 的 Wordpress 问题的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress Qtranslate 保持不要在单词搜索中保持语言

具有真实 QLocale 的 Canonical QTranslator 示例

删除 qtranslate 后页面空白

QTranslator 没有在 PyQt5 中持久安装

如何基于 QTranslator 更改 QPushButton 的图标字符串

为啥我需要一个 QTranslator 来在 Windows 上的 QMessageBox 中本地化按钮文本?