{"id":1230,"date":"2010-07-14T19:28:35","date_gmt":"2010-07-14T13:58:35","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1230"},"modified":"2010-07-15T10:13:45","modified_gmt":"2010-07-15T04:43:45","slug":"finding-users-locale-in-gsp","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/finding-users-locale-in-gsp\/","title":{"rendered":"Finding User&#8217;s Session Locale in GSP"},"content":{"rendered":"<p>There are situations where you might want to know the Locale of the user as set in the session in your GSP so that you can show text in a particular language\/manner. Here the Spring framework&#8217;s SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one of our GSPs to display the content accordingly.<\/p>\n<p>Please note that our use-case was like that in which we have fields for different languages in our domain classes. For example:<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"groovy\">\r\nclass MyDomain {\r\nString greetingEnglish\r\nString greetingFrench\r\n}\r\n<\/pre>\n<div><\/blockquote>\n<p>And now you have to conditionally display the object&#8217;s fields based on the user&#8217;s locale. If this were a static text, then the Grails in-built i18n support in the form of message bundles would be the ideal solution. But this is a different scenario.<\/p>\n<p>So let&#8217;s start by writing the following in your GSP :<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"html\">\r\n<g:set var=\"lang\" value=\"${session.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'}\"\/>\r\n\r\n<\/pre>\n<div><\/blockquote>\n<p>The above statement returns the ISO Language Code from the session object which is a map. In Grails, the map values can be retrieved by the &#8216;.&#8217; operator too by passing the keys. These codes are the lower-case, two-letter codes as defined by ISO-639. For example: &#8216;en&#8217; for English, &#8216;fr&#8217; for French and so on.<\/p>\n<p>Once you know the locale, you can show the conditional content on your page. It could be something like this:<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"html\">\r\n<g:if test=\"${lang.startsWith('en')}\">\r\n<h1> ${myDomainobject.greetingEnglish} <\/h1>\r\n<\/g:if>\r\n<g:elseif test=\"${lang.startsWith('fr')}\">\r\n<h1> ${myDomainobject.greetingFrench}<\/h1>\r\n<\/g:elseif>\r\n<g:else>\r\n<h1> Default Greeting <\/h1>\r\n<\/g:else>\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>Hope this will help !!!<\/p>\n<p>&#8211; Abhishek Tejpaul<\/p>\n<p>[Intelligrape Software Pvt. Ltd.]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are situations where you might want to know the Locale of the user as set in the session in your GSP so that you can show text in a particular language\/manner. Here the Spring framework&#8217;s SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one [&hellip;]<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[32,314,312,315,4841],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1230"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1230"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1230\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}