i18n
----

This document highlights the i18n-support in the engine. The
implementation is based on this document:

  * http://wiki.zope.org/zope3/ZPTInternationalizationSupport

With the exception of i18n:data and i18n:source, the implementation is
complete.

Let's provide German mock translations for all msgids:

  >>> from zope import component
  >>> from zope import interface

  >>> from zope.i18n.interfaces import ITranslationDomain
  >>> class MockTranslationDomain(object):
  ...     interface.implements(ITranslationDomain)
  ...
  ...     def translate(self, msgid, mapping=None, context=None,
  ...                   target_language=None, default=None):
  ...         if target_language != 'de':
  ...             return default
  ...
  ...         return "Mock translation of '%s'." % msgid

  >>> td = MockTranslationDomain()
  >>> component.provideUtility(td, ITranslationDomain, name="test")

Translation of tag contents
---------------------------

First, a simple example:

  >>> body = """\
  ... <div xmlns="http://www.w3.org/1999/xhtml"
  ...      xmlns:i18n="http://xml.zope.org/namespaces/i18n">
  ...   <span i18n:domain="test" i18n:translate="test_msgid">
  ...     Default
  ...   </span>
  ... </div>"""

First we need to turn the template into a callable:

  >>> from z3c.pt.pagetemplate import PageTemplate
  >>> template = PageTemplate(body)

Let's try rendering this template without passing a target language.
    
  >>> print template.render()
  <div>
    <span>
      Default
    </span>
  </div>

Now we'll render the template again---passing German as the language.

  >>> print template.render(target_language='de')
  <div>
    <span>Mock translation of 'test_msgid'.</span>
  </div>

Let's try infering the translation message id from the tag body.
  
  >>> body = """\
  ... <div xmlns="http://www.w3.org/1999/xhtml"
  ...      xmlns:i18n="http://xml.zope.org/namespaces/i18n">
  ...   <span i18n:domain="test" i18n:translate="">
  ...     Default
  ...   </span>
  ... </div>"""

Not passing a language:

  >>> template = PageTemplate(body)
  >>> print template.render()
  <div>
    <span>
      Default
    </span>
  </div>

Passing German:
  
  >>> print template.render(target_language='de')
  <div>
    <span>Mock translation of 'Default'.</span>
  </div>

We could also add a named block inside the tag.

  >>> body = """\
  ... <div xmlns="http://www.w3.org/1999/xhtml"
  ...      xmlns:i18n="http://xml.zope.org/namespaces/i18n">
  ...   <p i18n:domain="test" i18n:translate="">
  ...     <span i18n:name="count">18</span> bananas.
  ...   </p>
  ... </div>"""

  >>> template = PageTemplate(body)
  >>> print template.render()
  <div>
    <p>
      <span>18</span> bananas.
    </p>
  </div>
    
  >>> print template.render(target_language='de')
  <div>
    <p>Mock translation of '${count} bananas.'.</p>
  </div>
