{"id":1653,"date":"2010-09-14T13:14:52","date_gmt":"2010-09-14T07:44:52","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1653"},"modified":"2010-09-14T13:14:52","modified_gmt":"2010-09-14T07:44:52","slug":"grails-specific-locale-handling-change-language-and-return-to-the-same-page","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-specific-locale-handling-change-language-and-return-to-the-same-page\/","title":{"rendered":"GRAILS specific LOCALE Handling &#8211; Change Language and return to the same page"},"content":{"rendered":"<p>It&#8217;s common requirement  when we work on some portal &#8211; we need to provide support for various languages.<br \/>\nmessages.properties\/ResourceBundle is out of the scope of this post. Here I am just mentioning &#8211; <\/p>\n<h2 style=\"font-size:14px;\">how to switch to a different language taking an advantage of GRAILS<\/h2>\n<p>On your view (gsp) page add following &#8211; <\/p>\n<pre lang=\"gsp\">\r\n<%\r\nString targetUri = (request.forwardURI - request.contextPath);\r\n%>\r\n <g:link controller=\"lang\" action=\"lang\" params=\"[lang: 'en', targetUri: targetUri]\">English<\/g:link>\r\n <g:link controller=\"lang\" action=\"lang\" params=\"[lang: 'fr', targetUri: targetUri]\">French<\/g:link>\r\n <g:link controller=\"lang\" action=\"lang\" params=\"[lang: 'es', targetUri: targetUri]\">Spanish<\/g:link>\r\n.....\r\n.....\r\n<\/pre>\n<p>I just wanted to give you a rough idea. But you can create a custom tag for this &#8211; depending upon your requirement.<\/p>\n<p>Now the trick here is to send parameter with name &#8220;lang&#8221;. And GRAILS automatically recognizes it and set locale itself.<br \/>\nFollowing is the code at server side (controller\/action).<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\n\/\/import org.springframework.context.i18n.LocaleContextHolder\r\n\/\/import org.springframework.web.servlet.support.RequestContextUtils as RCU;\r\n\r\nclass LangController {\r\ndef lang = {\r\n       String targetUri = params.targetUri ? params.targetUri : \"\/\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ String language = params.lang ? params.lang : 'en'\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ LocaleContextHolder.setLocale(new Locale(language))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ RCU.getLocaleResolver(request).setLocale(request, response, new Locale(language))\r\n       redirect(uri: targetUri)\r\n}\r\n}\r\n<\/pre>\n<\/blockquote>\n<p>If you notice above, I have commented out few lines (with the help of grails internal implementation, we need to do nothing explicitly to switch to a different language). <\/p>\n<p>So in <strong>SINGLE LINE:<\/strong><\/p>\n<pre lang=\"html\">\r\nBase URL: http:\/\/example.com\/cont\/act\/id  <!--Grails will use \"messages.properties\" now -->\r\nLocale specific URL: http:\/\/example.com\/cont\/act\/id?lang=fr <!--Grails will use \"messages_fr.properties\" now -->\r\n<\/pre>\n<p>Hope it helps!<br \/>\n\ufeffSalil Kalia<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s common requirement when we work on some portal &#8211; we need to provide support for various languages. messages.properties\/ResourceBundle is out of the scope of this post. Here I am just mentioning &#8211; how to switch to a different language taking an advantage of GRAILS On your view (gsp) page add following &#8211; English French [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1653"}],"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\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}