On 22.11.2009, at 20:16, lo...@nic.fi wrote:

I'v got a problem translating text that is printed dynamically from a mysql 
database. I'm printing body part names and the related muscle names by using 
nested tal:repeat. In the inner loop I'd like to echo the muscle name and 
translate it if there's a translation available. Here's what I got so far:

  <li tal:repeat="bodyPart bodyParts">
      <tal:block tal:content="bodyPart/name" />
          <li tal:repeat="muscle php:bodyPart.Muscles">
              <tal:block i18n:translate="">
                  <span tal:content="muscle/name" />

.. but all that the inner loop prints is empty li elements. What am I doing 

Use this instead:

<tal:block i18n:translate="muscle/name">

i18n:translate="" builds translation key at compile time, ignoring tags and 
dynamic content.

i18n:translate="expression" will use value of the expression as translation key.

I've changed PHPTAL to reject all-whitespace translation keys. Version in SVN 
will now prevent this gotcha.

Oh I see. Then I'v got another guestion since I'm creating my own translator similar to this 
http://phptal.org/manual/en/split/i18n-translatekey.html and if I use <tal:block 
i18n:translate="muscle/name"> all the translations have to exists.

I want my translator to translate if there's a translation available and use 
the key text when there isn't. So I added a few lines to translate function. 
Does this seem like a good way of doing this?

public function translate($key, $htmlencode=true) {
        $value = @$this->_currentDomain[$key];
        if (empty($value)) {
                return $key;
   while (preg_match('/\${(.*?)\}/sm', $value, $m)) {
    list ($src, $var) = $m;
if (!array_key_exists($var, $this->_context)) {
           $err = sprintf('Interpolation error, var "%s" not set', $var);
           throw new Exception($err);
$value = str_replace($src, $this->_context[$var], $value);
   return $value;       


PHPTAL mailing list

Reply via email to