[Zope-CMF] Re: Internationlisation question

Philipp von Weitershausen philipp at weitershausen.de
Sun Feb 11 16:27:05 EST 2007

Charlie Clark wrote:
>> Zope 3 provides all sorts of localization functionality, including 
>> numbers, currency and calendaring.
> Look in zope.i18n.locales ...
> Yes, I can see what's there but I do not seem to be able to call it from 
> a page template. If I try the following example from
> http://wiki.zope.org/zope3/ZPTInternationalizationExamples
>   <p i18n:translate="longDateTime"
>      i18n:domain="string:calendar" i18n:target="string:de"
>      i18n:data="python:DateTime()">
>     Based on what the id specifies, the date time object is returned in
>     typical German format: Montag, 1. Januar 2001 (example)
>   </p>

This is quite outdated. It doesn't surprise me you're getting an error here.

Like Jean-Francois said, zope.i18n.locales has some good support for 
formatting datetime values (the Python variant, not Zope's DateTime 
variant). In Zope 3, this functionality is available through 
request.locale. In Zope 2, you will have to create the locale object 
yourself like so:

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

  languages = IUserPreferredLanguages(request)
  langs = languages.getPreferredLanguages()
  if langs:
      parts = (langs[0].split('-') + [None, None])[:3]
      parts = (None, None, None)

  locale = locales.getLocale(*parts)

Then you can format datetimes such as the 'modified' variable in the 
below example like so:

   <p i18n:translate="" tal:condition="modified"
      tal:define="formatter python:locale.dates.getFormatter('dateTime')">
     Last modified on
     <span i18n:name="modified_date"
           tal:replace="python:formatter.format(modified)" />

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

http://worldcookery.com -- Professional Zope documentation and training
Next Zope 3 training at Camp5: http://trizpug.org/boot-camp/camp5

More information about the Zope-CMF mailing list