Based on #2610436: Twig templates incorrectly use % trans % with arbitrary filters, the placeholder names in the following are entirely ignored and not part of the source string:

 {% trans %}
    random between -{{ data.degrees|abs }}° and {{ data.degrees|abs }}°
  {% endtrans %}

Comments

Gábor Hojtsy created an issue. See original summary.

gábor hojtsy’s picture

StatusFileSize
new785 bytes
gábor hojtsy’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: 2611584.patch, failed testing.

gábor hojtsy’s picture

Issue tags: +Drupal 8 compatibility

I tried finding some proof on whether this should be identified in the first place (based on API docs), but the allowed characters do not seem to be identified in the API docs.

gábor hojtsy’s picture

Status: Needs work » Needs review
StatusFileSize
new777 bytes

Did it depend on the filter?

gábor hojtsy’s picture

Aham, so extra filters will not be found rather :D However based on #2547639: Rename ambiguous "Menu" toggle in Bartik for accessibility, those would not work in trans anyway. So what should potx do is do throw a warning that the use of the trans block this way is pointless and skip the string entirely instead of detecting it in the wrong way.

gábor hojtsy’s picture

Title: Dotted twig placholder names are not found » Twig trans placeholders with extra filters result in broken strings
gábor hojtsy’s picture

StatusFileSize
new752 bytes

Status: Needs review » Needs work

The last submitted patch, 9: 2611584.patch, failed testing.

gábor hojtsy’s picture

Status: Needs work » Needs review
StatusFileSize
new3.58 KB

So this is a bit complex... Core's TwigTransTokenParser allows filters and getattr, and a couple other thigns:

  protected function checkTransString(\Twig_Node $body, $lineno) {
    foreach ($body as $node) {
      if (
        $node instanceof \Twig_Node_Text
        ||
        ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_Name)
        ||
        ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_GetAttr)
        ||
        ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_Filter)
      ) {
        continue;
      }
      throw new \Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
    }
  }

However in TwigNodeTrans, only the placeholder filter is explicitly supported. It does not seem to support any other filter.

So I think we should assume the @ prefix the same way TwigNodeTrans does and alter it if we find a placeholder marker. That would keep adding the variable name to the string even if an unexpected filter was found. Updated patch for this plus removing support for passthrough which was removed from core as well.

gábor hojtsy’s picture

StatusFileSize
new3.74 KB

Even more tests.

gábor hojtsy’s picture

StatusFileSize
new3.9 KB

One more test combination for fun :D Not 100% sure we expect it to work this way.

  • Gábor Hojtsy committed 42804bd on 7.x-3.x
    Issue #2611584 by Gábor Hojtsy, pp: Twig trans placeholders with extra...
gábor hojtsy’s picture

Status: Needs review » Fixed

Ok, let's get this out.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.