Zerho Zerho - 2 months ago 8
Twig Question

Twig interpolate variable into translated string

I have a Sylius instance with translations file configured and properly working

Right now just using a snippet like this in my

twig
:

{{ 'project.sylius.frontend.header.label'|trans }}


It retrieves the proper string from the
yaml
translation file:

project.sylius.frontend.header.title: Welcome here


What I want to know is, if there is the chance to store the strings with interpolation marks straight in the
yaml
,
something like this:

project.sylius.frontend.cart_number: you are going to buy %n candies
project.sylius.frontend.delivery: delivery will be between the %whatever and %whatever


And how to pass the variable in the the
twig
file

Answer

You can pass variable to the trans filter as first argument as array, as Example:

yaml:

project.sylius.frontend.cart_number: you are going to buy %number% candies

Twig.

{{ 'project.sylius.frontend.cart_number '|trans({'%number%': '4'}) }}

Take a look at the pluralization features.

Hope this help