spot_img
HomeJoomla教程Joomla教程菜鸟教程 在 ajax 中调用时 gettext 不翻译

Joomla教程菜鸟教程 在 ajax 中调用时 gettext 不翻译

spot_img

JasperAI 10000字免费额度试用

当在 gettext 调用(__()、_e() 等)中加载内容时,当我在浏览器中正常呈现页面(PHP 将内容传送到浏览器)时,它们可以很好地翻译,但是当我进行 AJAX 调用时,gettext 会不翻译。

我想也许是那个词,但是当我在从服务器正常加载的内容中使用 gettext 调用时(不是通过 ajax)它会翻译。 我正在使用 WPML,但我认为这与它没有任何关系?

有什么特别的我必须做或将 load_plugin_textdomain 函数调用添加到特定的操作挂钩吗?

它现在被添加到“plugins_loaded”操作中。 我按照他们的文档中的描述使用 WordPress ajax 方法,我得到了所有数据,只是没有得到 gettext 翻译。

WordPress divi主题

正如这里所建议的那样,是有问题的代码片段。 我没有包含 gettext .pot 和 .mo 文件,因为我知道这些文件有效(bc/其中的其他文本在插件本身的其余部分中得到翻译)。 我只是说明他们的名字是什么以及他们所在的位置与插件根相关。

//gettext files
// languages/my-plugin-fr_FR.pot
// languages/my-plugin-fr_FR.mo

//Javascript files
// js/main.js
(function($){

  function getResources() {
        $.ajax({
            type: "POST",
            url: ajax_object.ajaxurl,
            dataType: "json",
            data: {                    
                action: 'get_resources'               
            }
        })
        .done(function(data) {
            if (typeof data == "object" && data.hasOwnProperty("html")) {               
                $(".my-selector").empty().html(data.html);
            } else {
               alert("error on server");              
            }
        })
        .fail(function() {
            alert("error on server");  
        });
    }

  $(document).ready(function() {
     getResources();
  });

})(jQuery);
// end js/main.js


<?php
//MyPlugin class

class MyPlugin {    
  /// This value will be used as a unique identifier for translations
  public static $theme_domain_name="my-plugin";

  public function init() {
     if (!is_admin()) {         
      //any scripts and styles needed for the plugin to work in the front end
      add_action( 'wp_enqueue_scripts', array($this,'add_scripts_styles') );     
    }

    add_action('wp_ajax_get_resources', array($this,'ajax_get_resources'));
    add_action('wp_ajax_nopriv_get_resources', array($this,'ajax_get_resources'));
  }

  public function ajax_get_resources() {        
        $html = "";

        //just an example of returning post objects
        $posts = get_posts();

        $html .= $this->get_resources_html($posts);

        echo json_encode(array('html'=>$html));

        die();
  }

  public function add_scripts_styles() {
     wp_register_script('main-js', plugin_dir_url(__FILE__) . 'js/main.js', array('jquery'), '20131023' );
     wp_enqueue_script('main-js' );
     wp_localize_script('main-js', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
  }

  public function get_resources_html($resources) {
     $load_more_text = __('Load more',MyPlugin::$theme_domain_name);       

     //$html .= < do some other work on the resource posts provided >

     $html .= <<<LOAD
<a href="#">
    <span class="text-wrapper"><span class="text">
    {$load_more_text}
    </span></span>
</a>
LOAD;

    return $html;
  }
}
?>

<?php
//root plugin file my-plugin.php

require_once dirname( __FILE__ ) .'/MyPlugin.php';

$MyPlugin = new MyPlugin();    

add_action("plugins_loaded",function() {
    load_plugin_textdomain(MyPlugin::$theme_domain_name , false, dirname( plugin_basename(__FILE__) ) . '/languages/');
});

$MyPlugin->init();
?>

为时已晚,但供公众使用:

/* if qTranslate is installed */
/* set front locale for ajax calls requested from front-end */

function set_locale_for_frontend_ajax_calls() {

    if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX
        && substr( $_SERVER['HTTP_REFERER'], 0, strlen( admin_url() ) ) != admin_url() ) {

        load_theme_textdomain( 'your-theme-domain-name', get_template_directory() . '/languages’ );
    }
}

add_action( 'admin_init', 'set_locale_for_frontend_ajax_calls' );

add_action('wp_head','jsURLs');

function jsURLs(){

    global $q_config;

    ?><script type="text/javascript">
        /* <![CDATA[ */
        var ajaxurl      = "<?php echo admin_url('admin-ajax.php?lang='.$q_config['language']); ?>";
        /* ]]> */
    </script><?php

}

如果安装了 qTranslate,它对我有用,但如果不遵循,它可能会工作:

/* if qTranslate is not installed */
/* set front locale for ajax calls requested from front-end */

function set_locale_for_frontend_ajax_calls() {

    if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX
        && substr( $_SERVER['HTTP_REFERER'], 0, strlen( admin_url() ) ) != admin_url() ) {
        setlocale(LC_ALL, $_GET['lang']);
        load_theme_textdomain( 'your-theme-domain-name', get_template_directory() . '/languages’ );
    }
}

add_action( 'admin_init', 'set_locale_for_frontend_ajax_calls' );

add_action('wp_head','jsURLs');

function jsURLs(){

    global $q_config;

    ?><script type="text/javascript">
        /* <![CDATA[ */
        var ajaxurl      = "<?php echo admin_url('admin-ajax.php?lang='.get_locale()); ?>";
        /* ]]> */
    </script><?php

}

高质量外链购买

由于您使用的是 WPML,因此您可以获得当前语言,然后使用 lang 您的ajax请求中的参数:


<?php
    global $sitepress;
    $current_language = $sitepress->get_current_language(); // get current language
?>



(function($){

  function getResources() {
        $.ajax({
            type: "POST",
            url: ajax_object.ajaxurl,
            dataType: "json",
            data: {                    
                action: 'get_resources',
                lang:  <?php echo $current_language;  ?>
            }
        })


[...]

5分钟生成10篇英文软文article forge软件试用
siteground guangda
WordPress花园官方账号
WordPress花园隶致力于为广大跨境电商和独立站爱好者提供优质的WordPress教程、Woocommerce教程、Facebook、Twitter、tiktok、Instagram教程和谷歌SEO教程等资料和对应的建站推广服务。关注‘哟派出海’公众号了解最新资讯。粉丝福利:Shopline免费独立站建设14天优惠 商务合作: [email protected]
RELATED ARTICLES