问题
I'm using pikaday with the i18n option with names of the months and days in spanish, my problem is that in the input text/placeholder still shows the english names of said months.
This is my JS code:
var picker = new Pikaday(
{
numberOfMonths: 2,
field: document.getElementById('datepicker-2months'),
firstDay: 1,
minDate: new Date(2000, 0, 1),
maxDate: new Date(2020, 12, 31),
yearRange: [2000, 2020],
i18n: {
previousMonth : 'Mes anterior',
nextMonth : 'Mes siguiente',
months : ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
weekdays : ['Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado'],
weekdaysShort : ['Dom','Lun','Mar','Mier','Jue','Vie','Sab']
}
});
This should be straight forward but i'm puzzled as the pop-up calendar shows the names in the correct language, but not in the input placeholder.
回答1:
The Pikaday i18n normally uses moment.js
. If you don't want to use that, you have to do it yourself. You already provide the month and day names… but not the method that is used to fill in the input box value. This is done with the toString()
method:
var monthNames = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
picker = new Pikaday({
numberOfMonths: 2,
field: document.getElementById('datepicker-2months'),
firstDay: 1,
minDate: new Date(2000, 0, 1),
maxDate: new Date(2020, 12, 31),
yearRange: [2000, 2020],
i18n: {
previousMonth: 'Mes anterior',
nextMonth : 'Mes siguiente',
months : monthNames,
weekdays : ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado'],
weekdaysShort: ['Dom', 'Lun', 'Mar', 'Mier', 'Jue', 'Vie', 'Sab']
},
toString: function(date) {
var parts = [date.getDate(), monthNames[date.getMonth()], date.getFullYear()];
return parts.join(" ");
}
});
Try it on Codepen
If you want to be able to parse text from the input box to get the correct value in the calendar, you'll also need to implement the parse()
method… but at this point it may be easier to use moment.js
and let it handle the i18n for you.
This is all described in the Formatting section of the Pikaday doc.
回答2:
Your code should work fine.Check for other dependencies which may cause the issue.
Check below:
var picker = new Pikaday(
{
numberOfMonths: 2,
field: document.getElementById('datepicker-2months'),
firstDay: 1,
minDate: new Date(2000, 0, 1),
maxDate: new Date(2020, 12, 31),
yearRange: [2000, 2020],
i18n: {
previousMonth : 'Mes anterior',
nextMonth : 'Mes siguiente',
months : ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
weekdays : ['Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado'],
weekdaysShort : ['Dom','Lun','Mar','Mier','Jue','Vie','Sab']
}
});
<link rel="stylesheet" href="https://dbushell.com/Pikaday/css/pikaday.css">
<script src="https://dbushell.com/Pikaday/pikaday.js"></script>
<br><br><br><br><br><br><br><br><br><br><br>
<label for="datepicker-2months">Date:</label>
<input type="text" id="datepicker-2months">
回答3:
To achieve expected result , use below option
- Import Locale/es.js and Set Locale to spanish using moment - moment.locale('es') ;
- In locale section of Pikaday, use moment for months , weekdays and weekdaysShort instead of providing hard coded values
<input type="text" id="datepicker-2months" value="9 Octubre 2014">
<link rel="stylesheet" href="https://dbushell.com/Pikaday/css/pikaday.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.6.1/pikaday.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/locale/es.js"></script>
<script>
//Set Locale to spanish
moment.locale('es');
var picker = new Pikaday(
{
numberOfMonths: 2,
field: document.getElementById('datepicker-2months'),
firstDay: 1,
minDate: new Date(2000, 0, 1),
maxDate: new Date(2020, 12, 31),
yearRange: [2000, 2020],
format: 'D MMM YYYY',
i18n: {
previousMonth : 'Mes anterior',
nextMonth : 'Mes siguiente',
months : moment.localeData()._months,
weekdays : moment.localeData()._weekdays,
weekdaysShort : moment.localeData()._weekdaysShort
}
});
</script>
codepen for reference
来源:https://stackoverflow.com/questions/51756403/pikaday-i18n-still-showing-names-of-the-days-and-months-in-english