javascriptpikaday

Pikaday i18n still showing names of the days and months in english


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.


Solution

  • 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.