[schooltool-dev] Date in views

Marius Gedminas marius at pov.lt
Wed Dec 6 14:19:12 EST 2006


On Wed, Dec 06, 2006 at 07:09:28PM +0100, Jean-Fran=E7ois Roche wrote:
>   Wondering what's the best way to translate dates ? For the moment (in
> calendar) a date is printed: "Wednesday, 2006-12-06" , i need it in
> french and to the European format (Mercredi, 06-12-2006).
> =

>   What's the nicest solution ?
> =

>     * Work in the view so that calling from the template for ex:
> "view/currentDate" just return the date in the right language with the
> right format.
> =

>     * Call a method from a tool with the date that will translate the
> date. Ex: "python:datetimeTool.translate(view.currentDate)" in the
> template ? [solution used in Plone] Next question such a tool already
> exists ?
>   =

>     * Work with i18n:translate ?
> =

>     * Something else?

You could register a view (or several) for datetime.date objects and use
it wherever you need it, e.g.

    <span tal:content=3D"context/some_date/@@date">...</span>
    <span tal:content=3D"context/some_date/@@short_date">...</span>
    <span tal:content=3D"context/some_date/@@long_date">...</span>

and then keep the date formatting logic in one place.

The view would look like

    class DateFormatterView(BrowserView):
        zope.component.adapts(datetime.date, IRequest)

        def __call__(self):
            return self.context.isoformat() # or something

and be registered with

    <zope:view
        for=3D"*"
        name=3D"date"
        type=3D"zope.publisher.interfaces.browser.IBrowserRequest"
        factory=3D".someplace.DateFormatterView"
        permission=3D"zope.Public"
        />


Zope 3 has a locale-aware date formatter that integrates with the same
locale detection as used by the i18n:translate machinery.  You might
want to use it:

    class DateFormatterView(BrowserView):
        zope.component.adapts(datetime.date, IRequest)

        def __call__(self):
            formatter =3D self.request.locale.dates.getFormatter('dateTime',
                                                               'medium')
            return formatter.format(self.context)


Marius Gedminas
-- =

Go not unto the Usenet for advice, for you will be told both yea and nay (a=
nd
quite a few things that just have nothing at all to do with the question).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.schooltool.org/pipermail/schooltool-dev/attachments/2006=
1206/56c32781/attachment.pgp


More information about the Schooltool-dev mailing list