4. I18N namespace

Note: 'i18n' is a short name for 'internationalization'. This namespace allow template designers to specify some text zones that must be translated during template evaluation.

4.1. i18n:translate

This attribute defines some text part that must be translated using PHPTAL's translation system.

<div i18n:translate="string:welcome_message">Welcome here</div>

In the above example, PHPTAL will looks for a translation key named 'welcome_message' and will replace the content of the tag with the equivalent in currently requested language.

<div i18n:translate="">Welcome here</div>

This usage is a little different, no translation key is given, thus PHPTAL will use the content of the tag 'Welcome here' as the translation key. This is a regular translation if translation system knows the key 'Welcome here'.

If no translation is found, the key will be used as the translation result. That's why using readable message instead of keys may be a good choice.

Please note that the key to translate may be contained in a variable, to allow dynamic key selection.

<div tal:define="welcome random_welcome_message"/>
<did i18n:translate="welcome">...</div>

4.2. i18n:name

This attribute sets a translation variable value.

Translations may contain ${xxx} strings where "xxx" is the name of a variable that needs to be interpolated dynamically.

The value of this variable will be set to the tag and its content. If you don't need tag around the value, use tal:replace instead of tal:content. tal:omit-tag may help if the value is a concatenation of strings.

<span i18n:name="myVar" tal:content="some/path"/>
<!-- <span>${some/path}</span> -->

<span i18n:name="myVar" tal:replace="some/path"/>
<!-- ${some/path} -->

<span i18n:name="myVar">foo</span>
<!-- <span>foo</span> -->

<span i18n:name="myVar" tal:omit-tag="">foo</span>
<!-- foo -->

An example of i18n usage:

<div i18n:translate="">
  Welcome <span i18n:name="user" tal:replace="user/name"/>,
  you have <span i18n:name="mails" tal:replace="user/nbrMails"/>
  unread mails.
</div>

The translation key of this example will be:

"Welcome ${user}, you have ${mails} unread mails."

PHPTAL will replace ${user} with ${user/name} and ${mails} with ${user/nbrMails} in translation.

More information about I18N with PHPTAL is available in the PHP section of this book.