Localized dates with Zope 2 and Zope 3

Imagine, you want to display a date in a nicer way than just 2009-11-17. It’s not that difficult to localize it into e.g. German and output 17.11.2009. But what, if you need to display Mittwoch, 11.November 2009 instead?

Calling strftime(‘%A, %d.%B %Y’) on your date object would format the date with month- and week names, but in most cases display the names in English and not in German, as we @gocept need it.

Locales provide you with the nessecary functionality to format your datetime object in the users local format and language. You can choose from different templates (parameter length) or even create your own templates.

The example defines a method formatDate which takes a datetime object. It retrieves the users locale settings from the request, chooses a format template and then “mixes” everything together :-).

So, to get something like “Wednesday, 17.November 2009”, you will need to submit the length parameter full as shown in the above example:

class MyView(object):
def formatDate(self, datetime_date):
if not self.request._locale:
# zope2 doesn't initialize the locale itself
self.request.setupLocale()
formatter = self.request._locale.dates.getFormatter(
'date',  length='full')
return formatter.format(datetime_date)

Leave a Reply

%d bloggers like this: