[Zope-CMF] Re: Internationlisation question

Charlie Clark charlie at begeistert.org
Mon Feb 12 11:31:08 EST 2007

Am 11.02.2007 um 22:27 schrieb Philipp von Weitershausen:

> My book explains all of this in detail. I suggest picking up a copy :)

As luck would have it there was actually a copy in the shop so I'll  
be leafing through it in days to come.

As previously noted (although I'm waiting for my mails to come  
through on the mailing list - they're being rejected by zope.com for  
some reason - I wasn't quite able to use your example but had to put  
the work into an ExternalMethod

from zope.i18n.interfaces import IUserPreferredLanguages
from zope.i18n.locales import locales

import datetime, mx.DateTime as DateTime

def format(request, zopedate, what='date', fmt='long'):
     """Accepts a Zope DateTime or mx.DateTime object and returns the
     localised form. Default is long date time."""
     languages = IUserPreferredLanguages(request)
     langs = languages.getPreferredLanguages()
     if langs:
         parts = (langs[0].split('-') + [None, None])[:3]
         parts = (None, None, None)
     locale = locales.getLocale(*parts)
     formatter = locale.dates.getFormatter(what, fmt)
     if isinstance(zopedate, DateTime.DateTimeType):
         y, m, d, H, M, S = zopedate.tuple()[:6]
         y, m, d, H, M, S = zopedate.parts()[:6]
     dt = formatter.format(datetime.datetime(y, m, d, H, M, S))
     return dt

It's interesting to note that a Python datetime type is mandatory. I  
know that Marc-André is working mx.DateTime to make it compatible  
with Python datetime (essentially it's just missing .weekday but I  
was surprised to have problems with Zope DateTime.

And this is called like this

<span tal:content="python: context.scripts.locale(request, dates 
['date'], 'date', 'full')" />

ie., I couldn't pack this inside an i18n block as you suggested.  
Hopefully I'll find enlightenment in your book! But thanks for the  
help thus far.

Charlie Clark
Helmholtzstr. 20
D- 40215
Tel: +49-211-938-5360
GSM: +49-178-782-6226

More information about the Zope-CMF mailing list