Translating a Vaadin DatePicker

Translating a Vaadin DatePicker

Translating (internationalization of) the default English DatePicker in Vaadin (Flow) is quite straightforward.

I created a compact subroutine for effortless reuse in all of my date pickers.

// Declaration
private final DatePicker birthday = new DatePicker("Geburtstag");

// Set German DatePicker
birthday.setI18n(Parsers.setGermanDatePicker());

The code above calls the "setGermanDatePicker" method, which is located in the "Parsers" class in my specific case.

    /**
     * Set German DatePicker
     * Fill the DatePickerI18n with German values (First day Monday, etc.)
     * 
     * @return
     */
    public static DatePickerI18n setGermanDatePicker() {
        DatePickerI18n dpI18n = new DatePicker.DatePickerI18n();
        dpI18n.setDateFormat("dd.MM.yyyy");
        dpI18n.setFirstDayOfWeek(1);
        dpI18n.setToday("Heute");
        dpI18n.setCancel("Abbrechen");
        dpI18n.setMonthNames(List.of("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli",
                "August", "September", "Oktober", "November", "Dezember"));
        dpI18n.setWeekdays(List.of("Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"));
        dpI18n.setWeekdaysShort(List.of("So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"));
        return dpI18n;
    }

It should be simple to adapt the above code for other languages or more complex situations, such as translating based on the locale passed to the function. However, I only require German for this Vaadin app.