Javascript bootstrap-datepicker 全局设置语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26487765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:57:39  来源:igfitidea点击:

bootstrap-datepicker set language globally

javascriptangularjsdatepickerbootstrap-datepickerbootstrap-datetimepicker

提问by YoBre

I'm using bootstrap-datepaginatorthat inside uses bootstrap-datepicker. Utilizzanto bootstrap-datepicker as a single component, there are no problems to set the language, but the same is not true using bootstrap-datepaginator. How can I do?

我正在使用内部使用 bootstrap- datepicker 的 bootstrap-datepaginator。Utilizzanto bootstrap-datepicker 作为一个单独的组件,设置语言没有问题,但是使用 bootstrap-datepaginator 就不一样了。我能怎么做?

I'm trying to set the Italian language as a default for the entire project. In index.htmlI put the following script:

我正在尝试将意大利语设置为整个项目的默认语言。在index.html我把下面的脚本:

<script src="vendors/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<script src="vendors/jquery-ui/ui/i18n/datepicker-it.js"></script>

<script>
    $(function() {
        $( "#datepicker" ).datepicker( $.datepicker.regional[ "it" ] );
    });
</script>

But in the console I get these errors:

但是在控制台中我收到这些错误:

Uncaught TypeError: Cannot read property 'regional' of undefined datepicker-it.js:15
Uncaught TypeError: Cannot read property 'regional' of undefined (index):394

I tried everything but I'm going crazy!

我尝试了一切,但我快疯了!

Bootstrap Datepicker i18n

Bootstrap 日期选择器 i18n



I changed the code in this way:

我以这种方式更改了代码:

<script src="vendors/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<script src="vendors/bootstrap-datepicker/js/locales/bootstrap-datepicker.it.js"></script>


<script>
    $(function() {
        $.datepicker.setDefaults( $.datepicker.regional["it"] );
    });
</script>

but now the error is as follows:

但现在错误如下:

Uncaught TypeError: Cannot read property 'setDefaults' of undefined


With this fix:

有了这个修复:

$('.datepicker').datepicker({
    language: "it"
});

I haven't got errors in console but the language is always english by default

我在控制台中没有错误,但默认情况下语言总是英语

回答by mccannf

You can set default options for Bootstrap Datepicker by assigning them like so:

您可以通过像这样分配它们来为 Bootstrap Datepicker 设置默认选项:

$.fn.datepicker.defaults.language = 'it';

Example below:

下面的例子:

$(document).ready(function(){
     $.fn.datepicker.defaults.language = 'it';
});

$(document).ready(function(){
     $('.datepicker').datepicker();
});
<link href="https://cdn.jsdelivr.net/bootstrap.datepicker-fork/1.3.0/css/datepicker3.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/bootstrap.datepicker-fork/1.3.0/js/bootstrap-datepicker.js"></script>
<script src="https://cdn.jsdelivr.net/bootstrap.datepicker-fork/1.3.0/js/locales/bootstrap-datepicker.it.js"></script>
<input class="datepicker"/>

回答by Stephan van Hoof

Use jQuery's extend() utility method to merge your settings into the datepicker defaults object:

使用 jQuery 的 extend() 实用程序方法将您的设置合并到 datepicker 默认对象中:

$.extend( $.fn.datepicker.defaults, {format: 'dd-mm-yyyy', language: 'nl'} );

回答by Stephan van Hoof

<script src="/jquery/jquery.ui.datepicker.js" type="text/javascript"></script>
<script src="/jquery/jquery.ui.datepicker-it.js" type="text/javascript"></script>

<script>
$.datepicker.setDefaults( $.datepicker.regional["it"] );

$( "#data" ).datepicker();
</script>