Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    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

    . . .

    Comment (1)

    Add Your Comment

  • UVdesk
    nice article!
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close