{"id":26,"date":"2009-11-17T19:13:17","date_gmt":"2009-11-17T18:13:17","guid":{"rendered":"http:\/\/blog.gocept.com\/?p=26"},"modified":"2012-12-03T15:11:28","modified_gmt":"2012-12-03T14:11:28","slug":"localized-dates-with-zope-2-and-zope-3","status":"publish","type":"post","link":"https:\/\/blog.gocept.com\/2009\/11\/17\/localized-dates-with-zope-2-and-zope-3\/","title":{"rendered":"Localized dates with Zope 2 and Zope 3"},"content":{"rendered":"
Imagine, you want to display a date in a nicer way than just 2009-11-17<\/em>. It’s not that difficult to localize it into e.g. German and output 17.11.2009<\/em>. But what, if you need to display\u00a0Mittwoch, 11.November 2009<\/em> instead?<\/em><\/p>\n <\/em>Calling\u00a0strftime(‘%A, %d.%B %Y’) <\/em> 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.<\/span><\/span><\/p>\n Locales<\/em> provide you with the nessecary functionality to format your datetime<\/em> object in the users local format and language. You can choose from different templates (parameter length<\/em>) or even create your own templates.<\/p>\n The example defines a method formatDate<\/em> which takes a datetime<\/em> object. It retrieves the users\u00a0locale<\/em> settings from the request<\/em>, chooses a format template and then “mixes<\/em>” everything together :-).<\/p>\n So, to get something like “Wednesday, 17.November 2009”, you will need to submit the length<\/em> parameter full<\/em> as shown in the above example:<\/p>\nclass MyView(object):<\/span>\r\ndef formatDate(self, datetime_date):<\/span>\r\nif not self.request._locale:<\/span>\r\n# zope2 doesn't initialize the locale itself<\/span>\r\nself.request.setupLocale()<\/span>\r\nformatter = self.request._locale.dates.getFormatter(<\/span>\r\n'date', \u00a0length='full')<\/span>\r\nreturn formatter.format(datetime_date)<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"