Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Translation of block buggy? #7

Closed
Musiksammler opened this Issue · 4 comments

5 participants

@Musiksammler

Hello!

I have this html code:

<div>
  <p>Trans as Block:</p>
  {% trans %}
  Hello World!
  {% endtrans %}
</div>

The part between "trans" and "endtrans" is not translated. The compiled template looks like this:

<div>
  <p>Trans as Block:</p>
  ";
    // line 11
    echo gettext("");        // line 14
    echo "</div>
";

The string is not "Hello World!" but empty. I tested this with the current version of Twig and the il8n extension. Pulled it fresh from github just hours ago.
{% trans "Hello World!" %}
works as expected and even a
{% trans %}
...
{% plural %}
...
{% endtrans %}
block!?

@alecsammon

I submitted a possible solution to this in the devs discussion

http://groups.google.com/group/twig-devs/browse_thread/thread/97ccbc0afdef6326

The following isn't beautiful - but appears to work! Maybe you could
refactor?

    if(count($body) > 1)
    {
        foreach ($body as $node) {
            if ($node instanceof Twig_Node_Print) {
                $n = $node->getNode('expr');
                while ($n instanceof Twig_Node_Expression_Filter)
                {
                    $n = $n->getNode('node');
                }
                $msg .= sprintf('%%%s%%', $n->getAttribute('name'));

                $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
            } else {
                $msg .= $node->getAttribute('data');
            }
        }

        return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $node->getLine()))), $vars);
    }
    else
    {
        $msg .= $body->getAttribute('data');
        return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars);
    } 
@lmammino

I found the same issue and I developed a temporary bugfix doing something very similar to what @alecsammon did.
Here's my fix:

protected function compileString(Twig_NodeInterface $body)
{
    if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant) {
        return array($body, array());
    }

    $msg = '';
    $vars = array();
    if(sizeof($body))
    {
        foreach ($body as $node) {
            if ($node instanceof Twig_Node_Print) {
                $n = $node->getNode('expr');
                while ($n instanceof Twig_Node_Expression_Filter) {
                    $n = $n->getNode('node');
                }
                $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
                $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
            } else {
                $msg .= $node->getAttribute('data');
            }
        }
    }
    else
    {
        $msg = $body->getAttribute('data');
    }

    return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars);
}
@jrmgx

Same issue here, all the gettext functions are empty in compiled templates.
Any update soon ?

@fabpot
Owner

fixed

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.