PHP inside jQuery for WordPress translation with WPML

Is it possible to implement in jQuery the wordpress function ( _e ) to translate string with WPML ?
I want to put some text in some div with jQuery and be able to translate this string with WPML.

$('#MyDiv').html(<?php _e('Text to translate', 'woocommerce'); ?>);

It doesn t work, is there any way to do it ??
Thanks in advance

Read More

[ANSWER]

@Mukesh Ram, Thanks for your answer but I did not understand pretty well, by the way I have this code ;

$(function() { 
 var left = 35,
     $engraved = $('#MyDiv');

 $engraved.closest('li').append('<span id="engraved_counter"></span>'); 
 $('#engraved_counter').html("<?php __('Text to translate', 'woocommerce'); ?> <strong>" + left + "</strong>");

  $engraved.keyup(function () {
    left = 35 - $(this).val().length;

    if(left < 0){
      $('#engraved_counter').addClass("overlimit");
      left = 0;
    }if(left >= 0){
      $('#engraved_counter').removeClass("overlimit");
    }   

   $('#engraved_counter').html("<?php __('Text to translate', 'woocommerce'); ?> <strong>" + left + "</strong>");
  });
});

Can you tel me please how could I implement the code wich you gived to me ?

$translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );
 wp_localize_script( 'some_handle', 'object_name', $translation_array );

Thank you for your lightening.

[RESOLVED]

Just put in the order the code into your child function.php as mentionned :

function add_scripts_to_head() {
    wp_enqueue_script( 'custom-js', 'www.mysite.com/js/custom.js' );

    $translation_array = array( 
    'remain_text' => __( 'Maximum number of characters : ', 'woocommerce' ) 
    );
    wp_localize_script( 'custom-js', 'count_text', $translation_array );

}
add_action( 'wp_enqueue_scripts', 'add_scripts_to_head' );

And call it to the js file like this :

$('#MyDiv').html(count_text.remain_text);

Thanks !

Related posts

1 comment

  1. If you want to translate something with jQuery , you need to localize the script.

     $translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );
     wp_localize_script( 'some_handle', 'object_name', $translation_array );
    

Comments are closed.