Bootbox 4.1.0:如何将本地化字符串(如 Ok、Cancel)传递给 Bootbox 的确认

Bootbox 4.1.0: how to pass localized strings such as Ok, Cancel to Bootbox's confirm?

本文关键字:Bootbox Cancel 确认 Ok 字符串 本地化      更新时间:2023-09-26

在 Bootbox 3.2.0 中,我能够使用如下所示的字符串进行确认:

bootbox.confirm(
    confirm_string, 
    cancel_string, 
    yes_string,
    function(r) {           
        if (r) {
            //do something
        }
    }
);

我正在升级到 4.1.0,但上述函数调用出现错误。

根据 Bootbox 4.1.0 的文档 (http://bootboxjs.com/documentation.html),有两种方法可以调用 confirm:

bootbox.confirm(str message, fn callback)    
bootbox.confirm(object options)

我用消息字符串和回调函数测试了第一条方式,它可以工作。对于第二种方式,我能够按如下方式传递一个对象:

{
  message: message_string
  callback: function(r) {
    //do something
  }
}

如何以第二种方式传递"确定"、"取消"按钮的字符串?

谢谢和问候。

作为替代方案,这也可以直接使用 bootbox.confirm 来完成,如下所示:

bootbox.confirm({
    buttons: {
        confirm: {
            label: 'Localized confirm text',
            className: 'confirm-button-class'
        },
        cancel: {
            label: 'Localized cancel text',
            className: 'cancel-button-class'
        }
    },
    message: 'Your message',
    callback: function(result) {
        console.log(result);
    },
    title: "You can also add a title",
});

或使用本地化 - 选项,以更改每个默认值的所有按钮:

    bootbox.setDefaults({
          /**
           * @optional String
           * @default: en
           * which locale settings to use to translate the three
           * standard button labels: OK, CONFIRM, CANCEL
           */
          locale: "de"
    });

看到: http://bootboxjs.com/documentation.html, "帮助程序方法"

您可以使用"自定义对话框"(bootbox.dialog)来更改这些字符串。

bootbox.dialog({
  message: "Custom message",
  title: "Custom title",
  buttons: {
    danger: {
      label: "Custom Cancel",
      className: "btn-danger",
      callback: function() {
        //do something
      }
    },
    main: {
      label: "Custom OK",
      className: "btn-primary",
      callback: function() {
        //do something else
      }
    }
  }
});