Aller au contenu. | Aller à la navigation

Outils personnels


Vous êtes ici : Accueil / Articles / Creating a viewlet with five.grok

Creating a viewlet with five.grok

Par Vincent Fretin publié 07/07/2009 11:40, Dernière modification 28/01/2010 14:36
I show you how to use five.grok to easily insert a warning box when an ATDocument is expired.

I already talked about five.grok to create viewlets. In this article, I show you how to use five.grok to easily insert a warning box when an ATDocument is expired.

I assume you have the paster and i18ndude commands in your PATH. I generally install those eggs in a virtualenv like that:

$ virtualenv -p /usr/bin/python2.4 dev
$ . dev/bin/activate
(dev)$ easy_install ZopeSkel
(dev)$ easy_install i18ndude

Creating a Plone buidlout with grok

Start by creating a Plone 3.3rc3 buildout with paster create -t plone3_buildout and look at five.grok's buildout.cfg (svn co to see how to add latest five.grok to your buildout.

Finally your buildout.cfg should look like this:

parts =
extends =
versions = versions
find-links =
eggs =
develop =

five.grok = 1.0b1
grokcore.annotation = 1.0.1
grokcore.component = 1.7
grokcore.formlib = 1.1 = 1.0 = 1.0.1
grokcore.view = 1.7
grokcore.viewlet = 1.0
martian = 0.11 = 3.5.1 = 3.4.3
zope.component = 3.4.0
zope.i18n = 3.6.0
zope.securitypolicy = 3.4.1

recipe = plone.recipe.zope2install
url = ${versions:zope2-url}
skip-fake-eggs =

recipe = plone.recipe.zope2instance
zope2-location = ${zope2:location}
user = admin:admin
http-address = 8080

eggs =

zcml =

Creating a plone product

Now go to the src directory, create a plone product:

$ paster create -t plone my.viewlets
Enter namespace_package (Namespace package (like plone)) ['plone']: my
Enter package (The package contained namespace package (like example)) ['example']: viewlets
Enter zope2product (Are you creating a Zope 2 Product?) [False]:
Enter version (Version) ['1.0']:
Enter description (One-line description of the package) ['']: My viewlets with Grok

Edit my.viewlets/ to declare the egg as as Plone plugin. The end of the file should look like this:

target = plone

Return to parent directory and modify your buildout.cfg to add my.viewlets:

develop =

eggs =

Then execute:


Creating a viewlet with grok

Then go to src/my.viewlets/my/viewlets directory. All paths mentioned below are relative to this directory.

Content of the module:

from five import grok
from import IBelowContentTitle
from Products.ATContentTypes.interface.document import IATDocument


class ExpiredContentViewlet(grok.Viewlet):

Create a templates directory with a template file in it:

<dl tal:condition="context/isExpired"
    class="portalMessage warning">
  <dt i18n:domain="plone"
  <dd i18n:domain="my.viewlets"
    This document is expired since
    <span i18n:name="expiration_date"
          tal:define="plone_view context/@@plone"
          tal:replace="python:plone_view.toLocalizedTime(context.ExpirationDate(), long_format=1)">
      expiration date

Content of the configure.zcml file:

<configure xmlns=""

  <i18n:registerTranslations directory="locales" />

  <grok:grok package=".viewlets" />


For now, create an empty locales directory.

Start your instance with bin/instance fg, create a Page (ATDocument) and set the expire date to yesterday. Voilà!


Localization of the product

Now I'll show you how to localize your product product.

Create an script:

i18ndude rebuild-pot --pot locales/my.viewlets.pot --create my.viewlets .
i18ndude sync --pot locales/my.viewlets.pot locales/*/LC_MESSAGES/my.viewlets.po

Make the script executable:

chmod u+x

Execute the script:


It will failed for the the i18ndude sync because there is no translations yet, that's fine. The i18ndude rebuilt-pot command generate the locales/my.viewlets.pot file which contain all extracted strings to be translated.

Create a folder for the french translation:

mkdir -p locales/fr/LC_MESSAGES/

Initialize a french translation. I usually do this with msginit command (install the gettext package on Ubuntu to have the command):

msginit -i locales/my.viewlets.pot -o locales/fr/LC_MESSAGES/my.viewlets.po

This command do a copy of the pot and modify some headers in the po file. Now open locales/fr/LC_MESSAGES/my.viewlets.po and finish to modify the headers:

"Language-Code: fr\n"
"Language-Name: French\n"
"Domain: my.viewlets\n"

Replace all PACKAGE by my.viewlets. You can remove VERSION and remove the first comment "PLEASE EDIT THE LINES BELOW CORRECTLY". Perfect!

Now open the file with poedit for example and translate the only string you have. The french translation is:

Ce document est expiré depuis ${expiration_date}.

If you need to generate again the pot file and synchronize all translations, execute again ./

Restart your instance to see the french translation.