{"id":240,"date":"2015-10-26T21:52:59","date_gmt":"2015-10-26T20:52:59","guid":{"rendered":"http:\/\/www.christiaannieuwlaat.nl\/?p=240"},"modified":"2015-10-26T21:52:59","modified_gmt":"2015-10-26T20:52:59","slug":"translation-goodness-with-symfony2","status":"publish","type":"post","link":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/","title":{"rendered":"Translation goodness with symfony2"},"content":{"rendered":"<p>As the title would suggest this entry could be about the really nice translation mechanism provided by the symfony2 PHP framework (See <a href=\"http:\/\/www.symfony.com\/\" target=\"_blank\">http:\/\/www.symfony.com\/<\/a> ), but&#8230;.. it isn&#8217;t&#8230;.<\/p>\n<p>Well, technically it&#8217;s not quite about the translation mechanism itself, it&#8217;s about the ease you can use a cookie to switch locales ( and languages as far as translation goes ).<\/p>\n<p>In Symfony there is a translator component, which you can use to create translated pages on-the-fly, the only thing you need to do is to create language yaml files in the Bundle\/Resources\/translation folder which go by the following pattern &lt;-domain-&gt;.&lt;language&gt;.yml.<\/p>\n<p>Say we want to create texts for the &#8216;messages&#8217; domain, and we would like to support both english and dutch. We therefore need to create 2 files, called messages.en.yml and messages.nl.yml. In both files we create exactly the same keys, with the values being the text in the target language.<\/p>\n<p>The nice thing about symfony 2 is that the translator component automatically chooses the appropriate resource file to use for the translation. This is done according to the active locale ( or the default &amp; fallback settings in the symfony config file ). What I discovered today is that it&#8217;s possible to dynamically override the locale using a cookie and an Event listener..<\/p>\n<p>If you create an event-listener for the KernelRequest event ( using an onKernelRequest handler function ) if which you extract the targetlocale from a cookie, and insert it into the request object (using setLocale(&#8230;) ) you can change the locale on the fly. \u00a0You still need to add the custom eventlistener to the processing chain, which you can do in the services.yml file.<\/p>\n<p>At this moment I&#8217;m unable to post an example, but I will post an example soon, so please come back later&#8230; \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As the title would suggest this entry could be about the really nice translation mechanism provided by the symfony2 PHP framework (See http:\/\/www.symfony.com\/ ), but&#8230;.. it isn&#8217;t&#8230;. Well, technically it&#8217;s not quite about the translation mechanism itself, it&#8217;s about the ease you can use a cookie to switch locales ( and languages as far as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":243,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7,1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Translation goodness with symfony2 &raquo; Christiaan Nieuwlaat .nl<\/title>\n<meta name=\"description\" content=\"Use the Symfony 2 translation component, a cookie and an event listener to create a multilingual site in a flash.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Translation goodness with symfony2 &raquo; Christiaan Nieuwlaat .nl\" \/>\n<meta property=\"og:description\" content=\"Use the Symfony 2 translation component, a cookie and an event listener to create a multilingual site in a flash.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/\" \/>\n<meta property=\"og:site_name\" content=\"Christiaan Nieuwlaat .nl\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/christiaan.nieuwlaat\" \/>\n<meta property=\"article:published_time\" content=\"2015-10-26T20:52:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.christiaannieuwlaat.nl\/wp-content\/uploads\/2015\/10\/symfony2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"135\" \/>\n\t<meta property=\"og:image:height\" content=\"131\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"chris\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@krizzje\" \/>\n<meta name=\"twitter:site\" content=\"@krizzje\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"chris\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/\",\"url\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/\",\"name\":\"Translation goodness with symfony2 &raquo; Christiaan Nieuwlaat .nl\",\"isPartOf\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#website\"},\"datePublished\":\"2015-10-26T20:52:59+00:00\",\"dateModified\":\"2015-10-26T20:52:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef\"},\"description\":\"Use the Symfony 2 translation component, a cookie and an event listener to create a multilingual site in a flash.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.christiaannieuwlaat.nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Translation goodness with symfony2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#website\",\"url\":\"https:\/\/www.christiaannieuwlaat.nl\/\",\"name\":\"Christiaan Nieuwlaat .nl\",\"description\":\"IT Whizzkid behind the wheels of steel\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.christiaannieuwlaat.nl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef\",\"name\":\"chris\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g\",\"caption\":\"chris\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Translation goodness with symfony2 &raquo; Christiaan Nieuwlaat .nl","description":"Use the Symfony 2 translation component, a cookie and an event listener to create a multilingual site in a flash.","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:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/","og_locale":"en_US","og_type":"article","og_title":"Translation goodness with symfony2 &raquo; Christiaan Nieuwlaat .nl","og_description":"Use the Symfony 2 translation component, a cookie and an event listener to create a multilingual site in a flash.","og_url":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/","og_site_name":"Christiaan Nieuwlaat .nl","article_publisher":"https:\/\/www.facebook.com\/christiaan.nieuwlaat","article_published_time":"2015-10-26T20:52:59+00:00","og_image":[{"width":135,"height":131,"url":"https:\/\/www.christiaannieuwlaat.nl\/wp-content\/uploads\/2015\/10\/symfony2.png","type":"image\/png"}],"author":"chris","twitter_card":"summary_large_image","twitter_creator":"@krizzje","twitter_site":"@krizzje","twitter_misc":{"Written by":"chris","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/","url":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/","name":"Translation goodness with symfony2 &raquo; Christiaan Nieuwlaat .nl","isPartOf":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/#website"},"datePublished":"2015-10-26T20:52:59+00:00","dateModified":"2015-10-26T20:52:59+00:00","author":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef"},"description":"Use the Symfony 2 translation component, a cookie and an event listener to create a multilingual site in a flash.","breadcrumb":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2015\/10\/26\/translation-goodness-with-symfony2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.christiaannieuwlaat.nl\/"},{"@type":"ListItem","position":2,"name":"Translation goodness with symfony2"}]},{"@type":"WebSite","@id":"https:\/\/www.christiaannieuwlaat.nl\/#website","url":"https:\/\/www.christiaannieuwlaat.nl\/","name":"Christiaan Nieuwlaat .nl","description":"IT Whizzkid behind the wheels of steel","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.christiaannieuwlaat.nl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef","name":"chris","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g","caption":"chris"}}]}},"_links":{"self":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts\/240"}],"collection":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/comments?post=240"}],"version-history":[{"count":0,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts\/240\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/media\/243"}],"wp:attachment":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/media?parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/categories?post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/tags?post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}