{"id":43155,"date":"2016-03-15T13:23:57","date_gmt":"2016-03-15T13:23:57","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=43155"},"modified":"2019-01-08T08:12:23","modified_gmt":"2019-01-08T08:12:23","slug":"symfony-translate-variable-twig","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/","title":{"rendered":"Symfony How to Translate Variable in Twig"},"content":{"rendered":"<p>In Symfony we can translate strings\/ text in controllers and twig.<\/p>\n<pre class=\"brush:php\">\/\/webkul.demo.text = 'Webkul Demo Text' \/\/written in translation file\r\n\r\n$this-&gt;get('translator')-&gt;trans('webkul.demo.text');\r\n\r\n\/\/translate variable\r\n$webkulDemoText = 'webkul.demo.text';\r\n$this-&gt;get('translator')-&gt;trans($webkulDemoText);\r\n<\/pre>\n<p>In twig we use this<\/p>\n<pre class=\"brush:php\">{% trans %}webkul.demo.text{% trans %}<\/pre>\n<p>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<\/p>\n<pre class=\"brush:php\">{{ webkulDemoText|trans }}<\/pre>\n<p>Framework used \u2013 Symfony 2.7, <a href=\"https:\/\/twig.symfony.com\/\">Twig Official\u00a0Doc<\/a>\u00a0 &amp; <a href=\"http:\/\/symfony.com\/doc\/current\/book\/translation.html#twig-templates\" target=\"_blank\" rel=\"noopener\">Symfony Official Doc<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Symfony we can translate strings\/ text in controllers and twig. \/\/webkul.demo.text = &#8216;Webkul Demo Text&#8217; \/\/written in translation file $this-&gt;get(&#8216;translator&#8217;)-&gt;trans(&#8216;webkul.demo.text&#8217;); \/\/translate variable $webkulDemoText = &#8216;webkul.demo.text&#8217;; $this-&gt;get(&#8216;translator&#8217;)-&gt;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 <a href=\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":14,"featured_media":43102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,2707,2708,2709],"tags":[2710,2772,2711,2712],"class_list":["post-43155","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-symfony","category-symfony2","category-twig","tag-symfony","tag-symfony-2","tag-symfony2","tag-twig"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Symfony How to Translate Variable in Twig - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Symfony How to Translate Variable in Twig - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"In Symfony we can translate strings\/ text in controllers and twig. \/\/webkul.demo.text = &#039;Webkul Demo Text&#039; \/\/written in translation file $this-&gt;get(&#039;translator&#039;)-&gt;trans(&#039;webkul.demo.text&#039;); \/\/translate variable $webkulDemoText = &#039;webkul.demo.text&#039;; $this-&gt;get(&#039;translator&#039;)-&gt;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 [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-03-15T13:23:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-01-08T08:12:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Nikhil Malik\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nikhil Malik\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/\"},\"author\":{\"name\":\"Nikhil Malik\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/9a90e1b1a80142fc7445195c9ca8c8a4\"},\"headline\":\"Symfony How to Translate Variable in Twig\",\"datePublished\":\"2016-03-15T13:23:57+00:00\",\"dateModified\":\"2019-01-08T08:12:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/\"},\"wordCount\":64,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"keywords\":[\"symfony\",\"Symfony 2\",\"symfony2\",\"twig\"],\"articleSection\":[\"php\",\"Symfony\",\"Symfony2\",\"Twig\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/\",\"url\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/\",\"name\":\"Symfony How to Translate Variable in Twig - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"datePublished\":\"2016-03-15T13:23:57+00:00\",\"dateModified\":\"2019-01-08T08:12:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"width\":825,\"height\":260},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Symfony How to Translate Variable in Twig\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/9a90e1b1a80142fc7445195c9ca8c8a4\",\"name\":\"Nikhil Malik\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/335b26b1a56ac61c8dcf3b6fa4c8c3d1e68ed57cd9db7e6f5dfa97cc506cf0d5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/335b26b1a56ac61c8dcf3b6fa4c8c3d1e68ed57cd9db7e6f5dfa97cc506cf0d5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Nikhil Malik\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/nikhil\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Symfony How to Translate Variable in Twig - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/","og_locale":"en_US","og_type":"article","og_title":"Symfony How to Translate Variable in Twig - Webkul Blog","og_description":"In Symfony we can translate strings\/ text in controllers and twig. \/\/webkul.demo.text = 'Webkul Demo Text' \/\/written in translation file $this-&gt;get('translator')-&gt;trans('webkul.demo.text'); \/\/translate variable $webkulDemoText = 'webkul.demo.text'; $this-&gt;get('translator')-&gt;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 [...]","og_url":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-03-15T13:23:57+00:00","article_modified_time":"2019-01-08T08:12:23+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","type":"image\/png"}],"author":"Nikhil Malik","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Nikhil Malik"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/"},"author":{"name":"Nikhil Malik","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/9a90e1b1a80142fc7445195c9ca8c8a4"},"headline":"Symfony How to Translate Variable in Twig","datePublished":"2016-03-15T13:23:57+00:00","dateModified":"2019-01-08T08:12:23+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/"},"wordCount":64,"commentCount":1,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","keywords":["symfony","Symfony 2","symfony2","twig"],"articleSection":["php","Symfony","Symfony2","Twig"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/","url":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/","name":"Symfony How to Translate Variable in Twig - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","datePublished":"2016-03-15T13:23:57+00:00","dateModified":"2019-01-08T08:12:23+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","width":825,"height":260},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/symfony-translate-variable-twig\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Symfony How to Translate Variable in Twig"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/9a90e1b1a80142fc7445195c9ca8c8a4","name":"Nikhil Malik","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/335b26b1a56ac61c8dcf3b6fa4c8c3d1e68ed57cd9db7e6f5dfa97cc506cf0d5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/335b26b1a56ac61c8dcf3b6fa4c8c3d1e68ed57cd9db7e6f5dfa97cc506cf0d5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Nikhil Malik"},"url":"https:\/\/webkul.com\/blog\/author\/nikhil\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/43155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=43155"}],"version-history":[{"count":4,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/43155\/revisions"}],"predecessor-version":[{"id":157520,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/43155\/revisions\/157520"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/43102"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=43155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=43155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=43155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}