Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trans with syntax #7

Open
Luk1298 opened this issue Nov 25, 2022 · 10 comments
Open

Add trans with syntax #7

Luk1298 opened this issue Nov 25, 2022 · 10 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Luk1298
Copy link

Luk1298 commented Nov 25, 2022

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
Copy link
Owner

JBlond commented Nov 25, 2022

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

@JBlond
Copy link
Owner

JBlond commented Nov 25, 2022

@Luk1298 Do you have a complete example, please?

@Luk1298
Copy link
Author

Luk1298 commented Nov 25, 2022

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
Copy link
Author

Luk1298 commented Nov 25, 2022

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
Copy link
Owner

JBlond commented Nov 28, 2022

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();
        }

@JBlond JBlond added enhancement New feature or request help wanted Extra attention is needed labels Nov 28, 2022
@Luk1298
Copy link
Author

Luk1298 commented Nov 29, 2022

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
Copy link
Owner

JBlond commented Nov 29, 2022

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
Copy link
Author

Luk1298 commented Dec 29, 2022

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

@JBlond
Copy link
Owner

JBlond commented Dec 29, 2022

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

@JBlond
Copy link
Owner

JBlond commented Dec 29, 2022

You may also take a look at issue #6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants