【问题标题】:How do I allow a function to use jQuery.i18n?如何允许函数使用 jQuery.i18n?
【发布时间】:2020-03-05 17:20:56
【问题描述】:

我在让我的函数使用 jQuery.i18n 时遇到问题。它在 jQuery(document).ready(function ($) {...}); 内部运行良好,而不是在它外部运行。

jQuery(function($) {
  $.i18n().load({
    'en': '/path/to/i18n/en.json'
  });
});

jQuery(document).ready(function ($) {
    function payInfo('example@something.com', '12341234');

    console.log('Verification text test: ' + $.i18n('payments.verificationCode')); // works
});

function payInfo(address, invoice) {
    // translation does not work here
    swal({
        title: $.i18n('payments.verificationCode'),
        html: $.i18n('payments.verificationDetail', address),
        input: 'number',
        showCancelButton: true,
        confirmButtonText: $.i18n('global.confirm'),
        cancelButtonText: $.i18n('global.cancel'),
        showLoaderOnConfirm: true,
        inputPlaceholder: '0000',
        allowOutsideClick: false
     })
}

错误是TypeError: undefined is not an object (evaluating '$.i18n') - 并指向title: 行号。允许函数获取 i18n 翻译的正确方法是什么?

【问题讨论】:

    标签: javascript jquery internationalization


    【解决方案1】:

    您的 jQuery 不是全局定义的。尝试在.ready 中添加此内容:

    jQuery(document).ready(function ($) {
        window.$ = $; // This line
        function payInfo('example@something.com', '12341234');
    
        console.log('Verification text test: ' + $.i18n('payments.verificationCode')); // works
    });
    

    【讨论】:

      猜你喜欢
      • 2015-09-16
      • 2014-08-31
      • 2017-04-04
      • 1970-01-01
      • 2015-10-30
      • 2014-10-05
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多