Friday, February 27, 2009

Linking to translated content items from zope page templates

This document is now a how-to on plone.org.

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
    ##parameters=
    ##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=""
    tal:define="
    translated_object link_object/getContentTranslation"
    tal:content="translated_object/title"
    tal:attributes="href translated_object/absolute_url">
    [Title]
    </a>
    </span>



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).

2 comments:

marr said...

The link to this Plone howto is changed to http://plone.org/documentation/kb/create-links-to-translated-content/

Big thanks to your work. /marr/

Kees Hink said...

Thanks, i changed it!