JBlond / twig-trans

Twig 3 translation extension
https://github.com/JBlond/twig-trans
MIT License
12 stars 1 forks source link

Add trans with syntax #7

Open Luk1298 opened 1 year ago

Luk1298 commented 1 year ago

Hi there, is it possible for you, to add trans with {'key': value, ...} syntax for the trans tag? So translation first and then the replace of the keys inside the string.

This optional with parameter will be very helpful. (for older projects with this syntax)

Have a nice day.

JBlond commented 1 year ago

I wonder if the old version https://github.com/twigphp/Twig-extensions was able to do so. https://github.com/twigphp/Twig-extensions/issues/74

JBlond commented 1 year ago

@Luk1298 Do you have a complete example, please?

Luk1298 commented 1 year ago

The twig/extensions module have this feature but it supports only Twig 2. It is also archived. I got this error message with composer: twig/extensions v1.5.4 requires twig/twig ^1.27|^2.0 .... I am using Twig 3.4.3 and PHP 8.1.

I have this example: {% trans with {'%nameUser%':user.getName()} %}Eingeloggt als %nameUser%{% endtrans %}

or with multi line:

{% trans with {
    '%price1%': helper.price(100),
    '%price2%': helper.price(200),
} %}
    Rechnung: %price2% - %price1% = %price1%
{% endtrans %}
Luk1298 commented 1 year ago

Mhh well, now I see this message in composer: Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Maybe I can force the package to be installed.

JBlond commented 1 year ago

I found a way to recognize the "with" tag. I'm not sure yet, how to get that into the compiled template.

        $variables = null;
        if ($stream->nextIf(Token::NAME_TYPE, 'with')) {
            $variables = $this->parser->getExpressionParser()->parseExpression();
        }
Luk1298 commented 1 year ago

I found this in the twig/extensions module. Maybe you saw this too.

if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
    if ($stream->nextIf('with')) {
        $with = $this->parser->getExpressionParser()->parseHashExpression();
    } else {
        $body = $this->parser->getExpressionParser()->parseExpression();
    }
}

Maybe you are able to interate over the hash/json and replace everything before you return the value from the function to the template compiler (that replace the tag). It is only a guess.

JBlond commented 1 year ago

I got something to work on. I need to figure out, how to replace the placeholder.

        $variableArray = null;
        if ($stream->nextIf(Token::NAME_TYPE, 'with')) {
            $variables = $this->parser->getExpressionParser()->parseExpression()->getKeyValuePairs();
            foreach ($variables as $variable){
                /** @var \Twig\Node\Expression\ConstantExpression $variable['key'] */
                /** @var \Twig\Node\Expression\NameExpression $value['value'] */
                $variableArray[
                    $variable['key']->getAttribute('value')
                ] = $variable['value']->getAttribute('name');
            }
        }
Luk1298 commented 1 year ago

Did you found a solution? Maybe I can help you or we ask somebody else, who can help.

JBlond commented 1 year ago

Hi Luk, I didn't find an answer yet. You are welcome to help.

JBlond commented 1 year ago

You may also take a look at issue https://github.com/JBlond/twig-trans/issues/6