简体   繁体   English

本地化jQuery确认WordPress中的按钮

[英]Localization jQuery confirm buttons in WordPress

I want to localize jQuery confirm buttons in WordPress. 我想在WordPress中本地化jQuery确认按钮。 For this I coded in PHP file like this: 为此我在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' => __('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');

And jQuery codes: 和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'
                    }
                ]

    });

EDIT: After David Lee's answer, I realized, if I write "This is a String" instead of meta.yes , I get 0 and 1 again like here: 编辑:在David Lee的回答之后,我意识到,如果我写"This is a String"而不是meta.yes ,我会像这里一样得到01

        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'
                    }
                ]

I think firstly, I must correct my buttons parameters, but I don't know. 我想首先,我必须纠正我的按钮参数,但我不知道。

But this gives me like here: 但这让我喜欢这里:

在此输入图像描述

You see, my buttos are Yes and No , but the code shows 0 and 1 . 你看,我的buttos是YesNo ,但代码显示01 How do I correct this? 我该如何纠正?

try 尝试

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');

and in jQuery: 在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'
                    }
                ]

    });

i changed the order, you need to localize first then enqueue , also i changed the hook to admin_enqueue_scripts , and changed the name of the variables since no is a reserved one. 我改变了顺序,你需要首先进行本地化然后入enqueue ,我也将钩子更改为admin_enqueue_scripts ,并更改了变量的名称,因为no是保留的变量。

Try, 尝试,

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'
        },
    }
});

jQuery Confirm is just a plugin and not just one. jQuery Confirm只是一个插件而不仅仅是一个插件。 There are one more plugins named jQuery.confirm. 还有一个名为jQuery.confirm的插件。 Which is your plugin jQuery-Confirm or BootboxJS or others. 哪个是你的插件jQuery-ConfirmBootboxJS或其他。 I suggest that update your plugin. 我建议你更新你的插件。 Because Jaydip Nimavat's example works. 因为Jaydip Nimavat的榜样有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM