On Wednesday 16 May 2012 13:23 Robert Goldsmith wrote:
> Maybe I've missed something but my interpretation of your problem is that
> you want to add variable content into a translation string for an
> attribute.
> 
> How about something like:
> 
> <div i18n:attributes="title" title="titleTranslationKey">
>       <tal:block i18n:name="maxFilesize" tal:content="uploadMaxHumanFilesize" 
> />
> </div>
> 
> with a po file (assuming you are using gettext) of:
> 
> msgid "titleTranslationKey"
> msgstr "Drop photo to upload (max ${maxFilesize} MB)"
> 
> I am assuming here that the i18n:name key/value pairs are available for
> attribute translation in the same way they would be for content translation
> but I haven't seen why that wouldn't be the case.

Thanks for the suggestion. That - almost - works. One problem though.

 - We use PHP arrays for translations (don't ask me why), so using the ${var} 
notation gives undefined variable errors. I can hack around that by using 
{var} instead and use a modified PHPTAL_TranslationService::translate() 
method. Grabbed from DummyTranslator in the test dir ;-)

        function translate($key, $escape=true) {
            $translations = $this->getTranslations();
        if (array_key_exists($key, $translations)) {
            $v = $translations[$key];
        } else {
            $v = $key;
        }

        if ($escape) $v = htmlspecialchars($v);

        while (preg_match('/\{(.*?)\}/sm', $v, $m)) {
            list($src, $var) = $m;
            if (!isset($this->vars[$var])) return "!*$var* is not defined!";
            $v = str_replace($src, $this->vars[$var], $v);
        }
                return $v;
        }

and a PHPTAL_TranslationService::setVar method like this:

    public function setVar($key, $value) {
        $this->vars[$key] = $value;
    }

The entry in the translation array is:

"titleTranslationKey" => "Drop foto her for at uploade det (max. {maxFilesize} 
MB)"

My problem is that setVar() is called *after* translate() so the title in the 
resulting document ends up as "!*$var* is not defined!".
If I cheat and initialize the variables array:

    private $vars = array('maxFilesize'=>'512 MB');

The result is correctly "Drop foto her for at uploade det (max. 512 MB)".

Well, this was a very long-winding way of asking: Is there any way I can set 
the variable in the markup before using it?

-- 
Med venlig hilsen / Best Regards

Thomas Tanghus

_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to