Reading list Switch to dark mode

    Symfony How to Translate Variable in Twig

    Updated 8 January 2019

    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

    Searching for an experienced
    Symfony Company ?
    Find out More
    . . .

    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