Commit 42804bd8 authored by Gabor Hojtsy's avatar Gabor Hojtsy
Browse files

Issue #2611584 by Gábor Hojtsy, pp: Twig trans placeholders with extra...

Issue #2611584 by Gábor Hojtsy, pp: Twig trans placeholders with extra filters result in broken strings
parent c2608f1b
Loading
Loading
Loading
Loading
+10 −13
Changes for potx.inc: 10 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -1873,23 +1873,20 @@ function _potx_process_twig_trans_tag($stream, $file, $save_callback) {
      $token = $stream->next();
      $name[] = $token->getValue();

      $name = implode('.', $name);
      // Figure out if it's escaped, passthrough, or placeholder.
      // Figure out if it's escaped or placeholder.
      $prefix = '@';
      $token = $stream->next();
      // If the next thing we see is }}, this is escaped.
      if ($token->test(Twig_Token::VAR_END_TYPE)) {
        $text[] = "@$name";
      }
      // If the next thing we see is |, this is either passthrough or placeholder.
      elseif ($token->test(Twig_Token::PUNCTUATION_TYPE, '|')) {
      // If the next thing we see is |, this may be a placeholder. The only
      // "filter" supported is placeholder, so if there is any other filter
      // then we keep assuming it is to be escaped. (Even if later a
      // placeholder filter was attempted).
      if ($token->test(Twig_Token::PUNCTUATION_TYPE, '|')) {
        $token = $stream->next();
        if ($token->getValue() == 'passthrough') {
          $text[] = "!$name";
        }
        elseif ($token->getValue() == 'placeholder') {
          $text[] = "%$name";
        if ($token->getValue() == 'placeholder') {
          $prefix = '%';
        }
      }
      $text[] = $prefix . implode('.', $name);
    }
    $token = $stream->next();
  }
+4 −2
Changes for tests/potx.test: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -218,16 +218,18 @@ class PotxTestCase extends DrupalWebTestCase {
    $this->assertPluralID('Hello star.', 'Hello @count stars.');

    $this->assertMsgID('Escaped: @string');
    $this->assertMsgID('Pass-through: !string');
    $this->assertMsgID('Placeholder: %string');

    $this->assertMsgID('This @node.type is submitted by @author.name, and has a length of: @count. It contains: %node.numbers and @node.bad_text. Lets pass the bad text through: !node.bad_text.');
    $this->assertMsgID('This @node.type is submitted by @author.name, and has a length of: @count. It contains: %node.numbers and @node.bad_text.');

    $this->assertMsgIDContext('I have context.', 'Lolspeak');
    $this->assertNoMsgIDContext('I have no context.', 'zz');
    $this->assertMsgIDContext('I have context and another parameter.', 'Lolspeak');

    $this->assertMsgID('A multiline\n    trans block.');
    $this->assertMsgID('Test string with @extra_filter');
    $this->assertMsgID('Test string with @multiple_filters');
    $this->assertMsgID('Test string with %multiple_filters');
  }

  /**
+16 −6
Changes for tests/potx_test_8.html.twig: 16 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -61,11 +61,6 @@
    Escaped: {{ string }}
  {% endtrans %}
</div>
<div>
  {% trans %}
    Pass-through: {{ string|passthrough }}
  {% endtrans %}
</div>
<div>
  {% trans %}
    Placeholder: {{ string|placeholder }}
@@ -77,7 +72,7 @@
{% set count = token|length %}
<div>
  {% trans %}
    This {{ node.type }} is submitted by {{ author.name }}, and has a length of: {{ count }}. It contains: {{ node.numbers|placeholder }} and {{ node.bad_text }}. Lets pass the bad text through: {{ node.bad_text|passthrough }}.
    This {{ node.type }} is submitted by {{ author.name }}, and has a length of: {{ count }}. It contains: {{ node.numbers|placeholder }} and {{ node.bad_text }}.
  {% endtrans %}
</div>
{# Test trans tag with context. #}
@@ -105,3 +100,18 @@
    trans block.
  {% endtrans %}
</div>
<div>
{% trans %}
  Test string with {{ extra_filter|abs }}
{% endtrans %}
</div>
<div>
{% trans %}
  Test string with {{ multiple_filters|abs|placeholder }}
{% endtrans %}
</div>
<div>
{% trans %}
  Test string with {{ multiple_filters|placeholder|abs }}
{% endtrans %}
</div>