WordPress中的本地化jQuery确认按钮

时间:2019-03-02 04:21:39

标签: 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' => __('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回答之后,我意识到,如果我写"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'
                    }
                ]

我认为,首先,我必须更正按钮参数,但我不知道。

但是这给了我像这样:

enter image description here

您看到,我的臀部是YesNo,但是代码显示了01。我该如何纠正?

3 个答案:

答案 0 :(得分:3)

尝试

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

    });

我更改了顺序,首先需要本地化,然后enqueue,同时我也将钩子更改为admin_enqueue_scripts,并更改了变量的名称,因为no是保留的。

答案 1 :(得分:3)

尝试

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

答案 2 :(得分:1)

jQuery Confirm只是一个插件,而不仅仅是一个插件。还有一个名为jQuery.confirm的插件。您的插件jQuery-ConfirmBootboxJS或其他插件是哪个。我建议您更新插件。因为Jaydip Nimavat的示例有效。