Friday, February 27, 2009

Linking to translated content items from zope page templates

This document is now a how-to on

I had a situation where a custom footer links to a "Terms of Service" page. This page was a Page (Document) object with LinguaPlone enabled.

What happens is this:

  1. Set your browser's language to Dutch.

  2. Log in. Note that all texts are in Dutch.

  3. Click the link "Voorwaarden" (or whatever, it links to 'terms-of-service'

  4. You get the item in English instead of Dutch.

This is not desirable. We should get the translation, which has been created.

So how to link to it so it gets the right language? Two things:

  • Add a script getContentTranslation:
    ## Script (Python) "getContentTranslation
    ##bind context=context
    ##title=Get the preferred translation, if it's there.

    served_languages = context.portal_languages.listSupportedLanguages()
    boundLanguages = context.portal_languages.getLanguageBindings()
    prefLang = boundLanguages[0]
    return (hasattr(context, 'getTranslation') and context.getTranslation(prefLang)) or context;

  • In you TAL, do this:
            <span id=""
    tal:define="link_object python: context.portal_url.get(content_id, None);"
    tal:condition="python: link_object is not None"
    tal:attributes="id content_id">
    <a href=""
    translated_object link_object/getContentTranslation"
    tal:attributes="href translated_object/absolute_url">

This also takes care of inserting the translated title as the link title, and it checks for existence of the content object (whose id is defined in content_id).


marr said...

The link to this Plone howto is changed to

Big thanks to your work. /marr/

Kees Hink said...

Thanks, i changed it!