{"id":5478,"date":"2012-05-11T10:31:34","date_gmt":"2012-05-11T05:01:34","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=5478"},"modified":"2012-05-11T10:31:34","modified_gmt":"2012-05-11T05:01:34","slug":"change-user-preferred-locale-using-springs-sessionlocaleresolver","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/change-user-preferred-locale-using-springs-sessionlocaleresolver\/","title":{"rendered":"Change user preferred locale using spring&#8217;s SessionLocaleResolver"},"content":{"rendered":"<p>Here we are going to talk about a scenario where there are some records in database which are specific to locale and they need to be displayed as per user&#8217;s current locale at number of places.Also user can change its preferred locale.<\/p>\n<p>For this I preferred to set the user&#8217;s locale in the session object. There are two ways to do this :-<\/p>\n<p>1. Add attribute &#8220;lang&#8221; to the request and it will be available to session automatically.<\/p>\n<p>For example: whenever user clicks on the link, the language will change to English and the user will remain on the same page.<strong><br \/>\n<\/strong><\/p>\n<p>[groovy]<\/p>\n<p>&lt;g:link controller=&quot;${params.controller}&quot; action=&quot;${params.action}&quot; params=&quot;${params+[lang:&#8217;en&#8217;]}&quot;&gt;English&lt;\/g:link&gt;<br \/>\n\/*Add different languages here*\/<br \/>\n[\/groovy]<\/p>\n<p>2.Also set default locale in the session in case, it is not set.<\/p>\n<p>For that add following to any Filters :-<\/p>\n<p>[groovy]<\/p>\n<p>if (!session.&#8217;org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE&#8217;) {<br \/>\nLocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);<br \/>\nlocaleResolver.setLocale(request, response, new Locale(&#8216;en&#8217;));<\/p>\n<p>}<\/p>\n<p>[\/groovy]<\/p>\n<p>This sets the locale in spring&#8217;s SessionLocaleResolver class. Session always refer for the locale from here only.<\/p>\n<p>Now one can check the locale wherever session is available. For example in GSPs:<\/p>\n<p>[groovy]<\/p>\n<p>&lt;g:if test=&quot;${session.&#8217;org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE&#8217;.toString()==&#8217;en&#8217;}&quot;&gt;<\/p>\n<p>[\/groovy]<\/p>\n<p><strong>Note<\/strong>: This implementation is useful to set locale in session (for accessing many times). Otherwise locale is available directly from current request as given below:-<\/p>\n<p>[groovy]<br \/>\nimport org.springframework.web.servlet.support.RequestContextUtils as RCU<br \/>\nLocale locale=RCU.getLocale(request)<br \/>\n[\/groovy]<\/p>\n<p>Hope this helped!<br \/>\nNitesh Goel<br \/>\n[Intelligrape Software Pvt. Ltd.]<\/p>\n<div style=\"width: 1px;height: 1px;overflow: hidden\">RequestContextUtils<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here we are going to talk about a scenario where there are some records in database which are specific to locale and they need to be displayed as per user&#8217;s current locale at number of places.Also user can change its preferred locale. For this I preferred to set the user&#8217;s locale in the session object. [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":11},"categories":[7],"tags":[4840,32,108,816,312,818,315,4841],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5478"}],"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\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=5478"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5478\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=5478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=5478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=5478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}