Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DOC

Symfony How to Translate Variable in Twig

In Symfony we can translate strings/ text in controllers and twig.

//webkul.demo.text = 'Webkul Demo Text' //written in translation file

$this->get('translator')->trans('webkul.demo.text');

//translate variable
$webkulDemoText = 'webkul.demo.text';
$this->get('translator')->trans($webkulDemoText);

In twig we use this

{% trans %}webkul.demo.text{% trans %}

Previous twig syntax will not work for variables and will show error then how can we translate any variable in twig ? For this we have to use trans as a filter

{{ webkulDemoText|trans }}

Framework used – Symfony 2.7, Twig Official Doc  & Symfony Official Doc

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


1 comments

  • UVdesk
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home