Sugar icon indicating copy to clipboard operation
Sugar copied to clipboard

Is there a way to get month names?

Open thomasf1 opened this issue 7 years ago • 5 comments

Is there a good way to get the month names of the current locale?

I´ve been using this in version 2.0.0:

CalendarDefaults.monthNames = (m.capitalize() for m in Date.getLocale().months[0...12])

That now breaks as the Array with the month names is containing a bunch of undefined values in the latest Version... I guess it never was intended to be used anyway.

Is there any nice way to get the month names? Would be great to use the existing Sugar locale change for that :). And sorry for opening a issue for that :o.

thomasf1 avatar Jun 08 '17 16:06 thomasf1

So, this sounds really simple, but when you get into localizations it actually gets a bit complicated. The reason you're seeing undefined (this has been around for quite a while, so not sure which version you're coming from...) is that the months array is a sparse array that contains different forms of the month names. For English, it's basically just abbreviations (Aug for example, note that May would be undefined as it has no abbreviated form), however a good example of being complicated is Russian. To be able to properly parse Russian dates, both the nominative and genetive forms (and derived abbreviations as well) need to be included. What's worse is that depending on the use case you may need one form over the other. This makes a method like getMonths (which otherwise I would be in favor of adding) tricky, and really treads into territory that Sugar shouldn't be handling... I mean a method like getGenetiveMonthName that only applies to Russian is not... something I really want to do.

Anyway, if you're only need the standard forms for English (or indeed most languages), then you can just do .months.slice(0, 12) and it will give you what you need.

andrewplummer avatar Jun 08 '17 18:06 andrewplummer

Actually, thinking about it getMonths could take a second argument indicating the set you need.

andrewplummer avatar Jun 08 '17 18:06 andrewplummer

Funny enough, months.slice(0, 12) works nicely, months.slice(12, 24) for the short forms fails (both english and german (worked in 2.0.0).

Ah, the devil is always in the details. Damn Russians ;)... I see the dilemma there of a generic function....

thomasf1 avatar Jun 08 '17 20:06 thomasf1

To be more specific there:

Date.getLocale('en').months delivers ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "Jan", "Feb", "Mar", "Apr", undefined × 1, "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec", undefined × 8, "Sep"]

Date.getLocale('de').months is worse and delivers ["Januar", "Februar", "Mär", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember", "Jan", "Feb", "März", "Apr", undefined × 3, "Aug", "Sept", "Okt", "Nov", "Dez", undefined × 2, "Mar", undefined × 11, "Marz"]

I guess it´s from the quite clever way you pack the string when you unpack it.

thomasf1 avatar Jun 08 '17 20:06 thomasf1

So, a getMonths(locale, abbreviated) would be great. Might also help with testing... :)

thomasf1 avatar Jun 08 '17 21:06 thomasf1