Question

I am trying to use a self-defined formatting for format_date. I want the month's name in upper case, and for that I am using MMMM yyyy or LLLL yyyy. I expect something along the lines of April 2007. Here is my test program:

from datetime import date
from babel.dates import format_date

d = date(2007, 4, 1)
print format_date(d, 'MMMM yyyy', locale='es')
print format_date(d, 'LLLL yyyy', locale='es')
print format_date(d, 'MMMM yyyy', locale='it')
print format_date(d, 'LLLL yyyy', locale='it')
print format_date(d, 'MMMM yyyy', locale='en')
print format_date(d, 'LLLL yyyy', locale='en')

And here is the output:

abril 2007
abril 2007
aprile 2007
Aprile 2007
April 2007
April 2007

As you can see, the spanish (es) version is wrong: the month's name is in lower case, both for MMMM and LLLL. Why is that? Is this a bug in Babel? From the Italian output, I expected MMMM to mean lowercase, and LLLL to mean uppercase. What is the formatting code for capitalized month names? (This is not stated clearly in the documentation)

Was it helpful?

Solution

Spanish month names are lowercase:

Calendar: Names of the days of the week and months of the year use lower-case letters. Hoy es martes. (Today is Tuesday.) México celebra su independencia el 16 de septiembre. (Mexico celebrates its independence on September 16.)


You wanted to dig in deeper into why Babel has two format letters. Here you go.

You are right, this is not stated clearly in the docs, but by reading the source code, I was able to figure out that using L instead of M changes the context parameter for the get_month_names call. The source code shows that:

  • 'M' results in 'format'
  • 'L' results in 'stand-alone'

What do those mean? Well, digging in further I found that Babel uses CLDR data files. This is a format specified by the Unicode Consortium, and it turns out it's all explained there, including that 'M' and 'L' are standards.

Some languages use two different forms of strings (stand-alone and format) depending on the context. Typically the stand-alone version is the nominative form of the word, and the format version is in the genitive. Two different characters are used:

Field  Format  Stand Alone
Month  M       L

"Format" is apparently what you usually want, as it is intended to mean when the month appears alongside a date.

Finally, it does go on to include a note about capitalization:

If stand-alone forms are not needed for any grammatical reasons such as the above, and if your language would always capitalize a date symbol such as month name or weekday name when it appears by itself on a calendar page or as a menu item (but not when it appears in the middle of a sentence), then stand-alone forms may be used for capitalized versions of date symbols. However, there are other solutions for capitalizing date symbols which provide finer control over capitalization, see capitalization guidelines.

(emphasis added).

And as your test shows, English always capitalizes, Italian capitalizes for standalone (but not in sentences), and Spanish never uses capitalization. I would trust the locale data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top