{"id":1230,"date":"2010-07-14T19:28:35","date_gmt":"2010-07-14T13:58:35","guid":{"rendered":"https:\/\/2thenew.today\/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:\/\/2thenew.today\/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":20,"footnotes":""},"categories":[7],"tags":[32,314,312,315,4841],"class_list":["post-1230","post","type-post","status-publish","format-standard","hentry","category-grails","tag-gsp","tag-iso-639","tag-locale","tag-sessionlocaleresolver","tag-spring"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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&#039;s SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Abhishek Tejpaul\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Finding User\u2019s Session Locale in GSP | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"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&#039;s SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Finding User\u2019s Session Locale in GSP | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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&#039;s SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/#article\",\"name\":\"Finding User\\u2019s Session Locale in GSP | TO THE NEW Blog\",\"headline\":\"Finding User&#8217;s Session Locale in GSP\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhishek\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2010-07-14T19:28:35+05:30\",\"dateModified\":\"2010-07-15T10:13:45+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":4,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/#webpage\"},\"articleSection\":\"Grails, gsp, ISO-639, locale, SessionLocaleResolver, Spring\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/#listItem\",\"name\":\"Finding User&#8217;s Session Locale in GSP\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/#listItem\",\"position\":3,\"name\":\"Finding User&#8217;s Session Locale in GSP\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhishek\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhishek\\\/\",\"name\":\"Abhishek Tejpaul\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9aad3b7249e392a23dee22fda2eb99c64c33950f509a91e8c37e82efd8c3a388?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Abhishek Tejpaul\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/\",\"name\":\"Finding User\\u2019s Session Locale in GSP | TO THE NEW Blog\",\"description\":\"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's SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/finding-users-locale-in-gsp\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhishek\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhishek\\\/#author\"},\"datePublished\":\"2010-07-14T19:28:35+05:30\",\"dateModified\":\"2010-07-15T10:13:45+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Finding User\u2019s Session Locale in GSP | TO THE NEW Blog","description":"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's SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one","canonical_url":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/#article","name":"Finding User\u2019s Session Locale in GSP | TO THE NEW Blog","headline":"Finding User&#8217;s Session Locale in GSP","author":{"@id":"https:\/\/2thenew.today\/blog\/author\/abhishek\/#author"},"publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"},"datePublished":"2010-07-14T19:28:35+05:30","dateModified":"2010-07-15T10:13:45+05:30","inLanguage":"en-US","commentCount":4,"mainEntityOfPage":{"@id":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/#webpage"},"articleSection":"Grails, gsp, ISO-639, locale, SessionLocaleResolver, Spring"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.today\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/2thenew.today\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/#listItem","name":"Finding User&#8217;s Session Locale in GSP"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/#listItem","position":3,"name":"Finding User&#8217;s Session Locale in GSP","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.today\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.today\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.today\/blog\/author\/abhishek\/#author","url":"https:\/\/2thenew.today\/blog\/author\/abhishek\/","name":"Abhishek Tejpaul","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/9aad3b7249e392a23dee22fda2eb99c64c33950f509a91e8c37e82efd8c3a388?s=96&d=mm&r=g","width":96,"height":96,"caption":"Abhishek Tejpaul"}},{"@type":"WebPage","@id":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/#webpage","url":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/","name":"Finding User\u2019s Session Locale in GSP | TO THE NEW Blog","description":"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's SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.today\/blog\/author\/abhishek\/#author"},"creator":{"@id":"https:\/\/2thenew.today\/blog\/author\/abhishek\/#author"},"datePublished":"2010-07-14T19:28:35+05:30","dateModified":"2010-07-15T10:13:45+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.today\/blog\/#website","url":"https:\/\/2thenew.today\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Finding User\u2019s Session Locale in GSP | TO THE NEW Blog","og:description":"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's SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one","og:url":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/","og:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Finding User\u2019s Session Locale in GSP | TO THE NEW Blog","twitter:description":"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's SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one","twitter:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"1230","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 00:07:06","updated":"2024-02-29 09:34:09","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tFinding User\u2019s Session Locale in GSP\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.today\/blog"},{"label":"Grails","link":"https:\/\/2thenew.today\/blog\/category\/grails\/"},{"label":"Finding User&#8217;s Session Locale in GSP","link":"https:\/\/2thenew.today\/blog\/finding-users-locale-in-gsp\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/1230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/comments?post=1230"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/1230\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/media?parent=1230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/categories?post=1230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/tags?post=1230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}