Javascript WordPress中的本地化jQuery确认按钮
我想本地化WordPress中的jQuery确认按钮。为此,我在PHP文件中编写了如下代码:Javascript WordPress中的本地化jQuery确认按钮,javascript,jquery,wordpress,localization,Javascript,Jquery,Wordpress,Localization,我想本地化WordPress中的jQuery确认按钮。为此,我在PHP文件中编写了如下代码: function Confirmation() { // Register the script wp_register_script('eux-conf', '/js/eux-js.js'); // Localize the script with new data $translation_array = array( 'confirmation' =
function Confirmation()
{
// Register the script
wp_register_script('eux-conf', '/js/eux-js.js');
// Localize the script with new data
$translation_array = array(
'confirmation' => __('Confirmation', 'eux-loc'),
'message' => __('Do you want to delete this item?', 'eux-loc'),
'yes' => __('Yes', 'eux-loc'),
'no' => __('No', 'eux-loc')
);
// Enqueued script with localized data.
wp_enqueue_script('eux-conf');
wp_localize_script('eux-conf', 'meta', $translation_array);
}
add_action('admin_print_scripts', 'Confirmation');
和jQuery代码:
jQuery('.delete').click(function()
{
jQuery.confirm({
'title': meta.confirmation,
'message': meta.message,
buttons: [
{
text: meta.yes,
classes: 'widget btn primary',
id: "yes",
onclick: function() {
}
},
{
text: meta.no,
classes: 'widget btn secondary',
id: "no",
onclick: 'close'
}
]
});
编辑:在David Lee的回答之后,我意识到,如果我写“这是一个字符串”
,而不是meta。是的,我会得到0
和1
,如下所示:
buttons: [
{
text: "This is a String",
classes: 'widget btn primary',
id: "yes",
onclick: function() {
}
},
{
text: meta.no,
classes: 'widget btn secondary',
id: "no",
onclick: 'close'
}
]
我想首先,我必须纠正我的按钮参数,但我不知道
但这让我觉得:
buttons: [
{
text: "This is a String",
classes: 'widget btn primary',
id: "yes",
onclick: function() {
}
},
{
text: meta.no,
classes: 'widget btn secondary',
id: "no",
onclick: 'close'
}
]
你看,我的按钮是Yes
和No
,但代码显示0
和1
。如何更正此问题?试试看
function Confirmation()
{
// Register the script
wp_register_script('eux-conf', '/js/eux-js.js');
// Localize the script with new data
$translation_array = array(
'confirmation' => __('Confirmation', 'eux-loc'),
'message' => __('Do you want to delete this item?', 'eux-loc'),
'yes_s' => __('Yes', 'eux-loc'),
'no_s' => __('No', 'eux-loc')
);
// Enqueued script with localized data.
wp_localize_script('eux-conf', 'meta', $translation_array);//this one first
wp_enqueue_script('eux-conf');
}
add_action('admin_enqueue_scripts', 'Confirmation');
在jQuery中:
jQuery('.delete').click(function()
{
jQuery.confirm({
'title': meta.confirmation,
'message': meta.message,
buttons: [
{
text: meta.yes_s,
classes: 'widget btn primary',
id: "yes",
onclick: function() {
}
},
{
text: meta.no_s,
classes: 'widget btn secondary',
id: "no",
onclick: 'close'
}
]
});
我更改了顺序,您需要先本地化,然后排队
,我还将钩子更改为管理队列脚本
,并更改了变量的名称,因为否
是保留变量。尝试
jQuery.confirm({
'title': meta.confirmation,
'message': meta.message,
buttons: {
yes: {
text: meta.yes_s, // OR text: "'"+meta.yes_s+"'",
classes: 'widget btn primary',
id: "yes",
onclick: function() {
}
},
no: {
text: meta.no_s, // OR text: "'"+meta.no_s+"'",
classes: 'widget btn secondary',
id: "no",
onclick: 'close'
},
}
});
jqueryconfirm只是一个插件,而不仅仅是一个插件。还有一个名为jQuery.confirm的插件。哪一个是你的插件或其他插件。我建议更新你的插件。因为Jaydip Nimavat的例子是有效的。一行中有一个打字错误-wp\u register\u脚本('eux-conf',/js/eux js.js')
缺少引号,应该是wp\u register\u脚本('eux-conf','/js/eux js.js')代码>(在第二个参数之前引用)。@JackBashford,谢谢,这是一个复制粘贴错误。我正在编辑。不幸的是,结果没有改变。。。现在我意识到,如果你写这是一个字符串,而不是meta。是的,你会再次得到0
和1
。你能做console.log(meta)吗代码>并将其显示到您的问题中?