最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - bootstrap-datepicker set language globally - Stack Overflow

programmeradmin1浏览0评论

I'm using bootstrap-datepaginator that 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?

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

<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


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

I'm using bootstrap-datepaginator that 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?

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

<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


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

Share Improve this question edited Oct 21, 2014 at 15:11 YoBre asked Oct 21, 2014 at 13:20 YoBreYoBre 2,5305 gold badges28 silver badges37 bronze badges 3
  • 1 I think you are missing an include for the jQuery UI datepicker - you only include the localized content for it. – RaYell Commented Oct 21, 2014 at 13:23
  • 4 You're using the bootstrap datepicker, not the jQueryUI one. – Rory McCrossan Commented Oct 21, 2014 at 13:23
  • stackoverflow.com/questions/19382189/… – Dave Commented Oct 21, 2014 at 14:01
Add a comment  | 

3 Answers 3

Reset to default 31

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

$.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"/>

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

$.extend( $.fn.datepicker.defaults, {format: 'dd-mm-yyyy', language: 'nl'} );
<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>
发布评论

评论列表(0)

  1. 暂无评论