Bootstrap Datepicker Locale with MomentJS
Asked Answered
S

2

8

I am using a Bootstrap Datepicker available here and everything is working fine. However, I want to localize the calendar given a user's preferred language. I've set the following when I initialize the datepicker:

locale: 'fr'

However, I get a console error that reads:

Uncaught TypeError: locale() locale fr is not loaded from moment locales!

I am including MomentJS in my project:

<script src="/vendors/moment/min/moment.min.js"></script>

This is my first implementation so I feel like I'm missing something simple but just can't figure it out.

Succubus answered 3/3, 2016 at 17:11 Comment(0)
T
9

As the error messages says, you just have to include the desired locale to solve your issue. If you need only fr locale, you can add something like this:

<script src="/vendors/moment/locale/fr.js"></script>

after moment import.

EDIT:

If you need multiple locales you can import locales.js instead of fr.js. Moreover moments provides a single file, moment-with-locales.min.js, with moment library + all supported locales.

If you want to dynamically set locale for you picker, you can use locale function:

// Example with german locale
var localeString = 'de';
$('#your-picker-id').data("DateTimePicker").locale(localeString);
Thistle answered 3/3, 2016 at 19:52 Comment(1)
Thank you for the help! So that resolved the error but now the datepicker is in French regardless of whether or not I specify the locale. I'll keep trying!Succubus
H
13

Replace

/moment/min/moment.min.js

by

/moment/min/moment-with-locales.js

and it will work

Hooge answered 23/8, 2016 at 13:31 Comment(0)
T
9

As the error messages says, you just have to include the desired locale to solve your issue. If you need only fr locale, you can add something like this:

<script src="/vendors/moment/locale/fr.js"></script>

after moment import.

EDIT:

If you need multiple locales you can import locales.js instead of fr.js. Moreover moments provides a single file, moment-with-locales.min.js, with moment library + all supported locales.

If you want to dynamically set locale for you picker, you can use locale function:

// Example with german locale
var localeString = 'de';
$('#your-picker-id').data("DateTimePicker").locale(localeString);
Thistle answered 3/3, 2016 at 19:52 Comment(1)
Thank you for the help! So that resolved the error but now the datepicker is in French regardless of whether or not I specify the locale. I'll keep trying!Succubus

© 2022 - 2024 — McMap. All rights reserved.