在jQuery中可以实现wordpress函数( _e )来用WPML翻译字符串吗?我想用jQuery在一些div中放一些文本,并且能够用WPML翻译这个字符串。
$('#MyDiv').html(<?php _e('Text to translate', 'woocommerce'); ?>);这不管用,有什么办法吗?提前感谢
答案
@Mukesh Ram,谢谢你的回答,但我不太明白,顺便说一下,我有这段代码;
$(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>");
});
});你能给我打电话吗?你给我的代码怎么实现?
$translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );
wp_localize_script( 'some_handle', 'object_name', $translation_array );谢谢你的闪电。
解析
只需按顺序将代码放入您的子function.php中,如下所述:
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' );并将其调用到js文件中,如下所示:
$('#MyDiv').html(count_text.remain_text);谢谢!
发布于 2016-05-11 14:18:37
如果你想用jQuery翻译一些东西,你需要本地化脚本。
$translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );
wp_localize_script( 'some_handle', 'object_name', $translation_array );https://stackoverflow.com/questions/37165250
复制相似问题