When loading content in gettext calls (__(), _e(), etc.) they translate fine when I render the page in the browser normally (PHP delivers the content to the browser), however when I make AJAX calls, the gettext does not translate.
I thought maybe it was that word specifically, but when I use the gettext call in content that is loaded from the server normally (not through ajax) it translates. I'm using WPML, but I don't think that has anything to do with it?
Is there anything special I have to do or add the load_plugin_textdomain function call to a specific action hook?
It's added to the "plugins_loaded" action right now. I use WordPress ajax methods as described in their docs and I get all the data, just don't get gettext translations.
As advised here are snippets of the code that has the problem. I did not incldue the gettext .pot and .mo files, as I know those work (bc/ other text in there gets translated in the rest of the plugin itself). I just state what their name is and where they reside relevant to the plugin root.
//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();
?>
When loading content in gettext calls (__(), _e(), etc.) they translate fine when I render the page in the browser normally (PHP delivers the content to the browser), however when I make AJAX calls, the gettext does not translate.
I thought maybe it was that word specifically, but when I use the gettext call in content that is loaded from the server normally (not through ajax) it translates. I'm using WPML, but I don't think that has anything to do with it?
Is there anything special I have to do or add the load_plugin_textdomain function call to a specific action hook?
It's added to the "plugins_loaded" action right now. I use WordPress ajax methods as described in their docs and I get all the data, just don't get gettext translations.
As advised here are snippets of the code that has the problem. I did not incldue the gettext .pot and .mo files, as I know those work (bc/ other text in there gets translated in the rest of the plugin itself). I just state what their name is and where they reside relevant to the plugin root.
//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();
?>
Share
Improve this question
edited May 15, 2019 at 15:48
ludovico
1498 bronze badges
asked Nov 8, 2013 at 21:57
ako977ako977
1711 silver badge3 bronze badges
6
- Are you using ajax properly? – gmazzap Commented Nov 8, 2013 at 22:02
- Yes I use the wordpress recommended ajax methods and call them in both add_action('wp_ajax_my_action', 'my_action_callback'); and add_action('wp_ajax_nopriv_my_action', 'my_action_callback'); I also use the action property to set 'my_action' in my POST sent from javascript so the aforementioned callbacks get run. All my code runs well and the content is sent to the browser and rendered. I'm able to call wordpress functions like get_posts, $wpdb, etc. Just the gettext doesn't get translated. – ako977 Commented Nov 8, 2013 at 23:30
- 1 Can you post a SSCCE? Also, technical clarifications belong on the Question, not in Comments, you're free to edit it whenever needed. – brasofilo Commented Nov 9, 2013 at 11:01
- 1 Deactivate wpml and see if that fixes your problem. – John P Bloch Commented Feb 3, 2014 at 22:05
- Any progress on that question? – kaiser Commented Feb 18, 2014 at 2:37
2 Answers
Reset to default 1it is too late but for public use:
/* 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
}
it works for me if qTranslate is installed but if not following maybe work:
/* 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
}
Since you're using WPML, you can get the current language and then pass it using the lang
parameter in your ajax request:
<?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; ?>
}
})
[...]