commenthol / date-holidays

worldwide holidays
https://commenthol.github.io/date-holidays/
Other
918 stars 238 forks source link

Include month and week day names in native languages #409

Closed fallenartist closed 1 year ago

fallenartist commented 1 year ago

A big task but it would be great to be able to get names of months and week days according to language setting, e.g:

Date.locale = {
    de: {
        name: "Deutsch",
        month_names: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
        month_names_short: ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'],
        day_names: ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'],
        day_names_short: ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']
    },
    en: {
        name: "English",
        month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
        month_names_short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        day_names: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
        day_names_short: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    pl: {
        name: "Polski",
        month_names: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'],
        month_names_short: ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paź', 'Lis', 'Gru'],
        day_names: ['Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela'],
        day_names_short: ['Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'Sb', 'Nd']
    },
    pt: {
        name: "Portugese",
        month_names: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
        month_names_short: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
        day_names: ['Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado', 'Domingo'],
        day_names_short: ['Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab', 'Dom']
    }
};
fallenartist commented 1 year ago

I should have explained this would be to override native toLocaleString which can give unsatisfying results. I've ended up writing my own functions that replace some of the results:

Date.prototype.getMonthNameShort = function(lang) {
    lang = lang || 'en';
    return lang in Date.locale ? 
        Date.locale[lang].month_names_short[this.getMonth()] :
        this.toLocaleString(lang, {month: "short"});
};

Date.prototype.getDayNameShort = function(lang) {
    lang = lang || 'en';
    return lang in Date.locale ? 
        Date.locale[lang].day_names_short[this.getDay()] : 
        this.toLocaleString(lang, {weekday: "short"});
};

Date.locale = {
    pl: { // Polish
        month_names_short: ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paź', 'Lis', 'Gru'],
        day_names_short: ['Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'Sb', 'Nd']
    },
    pt: { // Portugese
        month_names_short: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
        day_names_short: ['Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab', 'Dom']
    }
};

// const customMonthName = holidayList[0].start.getMonthNameShort(lang);
commenthol commented 1 year ago

Hi @fallenartist, thanks for sharing. I'd rather keep things related to this library, which is take care of holidays (and probably the names related to it). Therefor I'd recommend to either create a own library which takes care on calendar names or use Intl. Hence closing this one...