{"id":62,"date":"2014-07-23T20:09:45","date_gmt":"2014-07-23T20:09:45","guid":{"rendered":"http:\/\/www.morematter.net\/?p=62"},"modified":"2014-07-23T20:09:45","modified_gmt":"2014-07-23T20:09:45","slug":"accessing-i18n-message-in-grails-service","status":"publish","type":"post","link":"https:\/\/experimentalism.net\/morematter\/accessing-i18n-message-in-grails-service\/","title":{"rendered":"Accessing i18n message in Grails service"},"content":{"rendered":"<p>Seems (to me) like an obvious need &#8211; referencing a translatable message in a service in a Grails app. But you can&#8217;t just use g:message &#8211; well, I&#8217;ve seen some workarounds that let you access the g namespace, but that doesn&#8217;t really seem advisable. I just want message handling. I found several references to the use of getMessage, e.g. this one on <a href=\"http:\/\/stackoverflow.com\/questions\/8684712\/grails-how-to-access-i18n-from-controller-or-service\" target=\"_blank\">StackOverflow<\/a>, but I had a little trouble implementing properly, so thought I&#8217;d make a note.<\/p>\n<p>Requirements:<\/p>\n<pre>import org.springframework.context.MessageSource<\/pre>\n<pre>def MessageSource messageSource<\/pre>\n<p>Then you can call getMessage, like this:<\/p>\n<pre>messageSource.getMessage(\"my.message.key\", null, \"default\", myLocale)<\/pre>\n<p>Note two things: 1) null for args &#8211; I initially thought I could pass [] if I had no args, but that does not work, so pass null instead; and 2) myLocale &#8211; in my case I could probably just use Locale.default, but in keeping with the principle of il8n, I pass the locale as a variable into the service. So, in my controller I have (simplified):<\/p>\n<pre>import org.springframework.web.servlet.support.RequestContextUtils as RCU<\/pre>\n<pre>class MyController {<\/pre>\n<pre>\u00a0 def myService<\/pre>\n<pre>  def myAction() {<\/pre>\n<pre>\u00a0\u00a0\u00a0 myService.myMethod(RCU.getLocale(request))<\/pre>\n<pre>\u00a0 }<\/pre>\n<pre>}<\/pre>\n<p>And in MyService I have (simplified):<\/p>\n<pre>import org.springframework.context.MessageSource<\/pre>\n<pre>class MyService {<\/pre>\n<pre>\u00a0 def MessageSource messageSource<\/pre>\n<pre>\u00a0 def myMethod(Locale locale) {<\/pre>\n<pre>\u00a0\u00a0\u00a0 println messageSource.getMessage(\"message.key\", null, \"\", locale)<\/pre>\n<pre>\u00a0 }<\/pre>\n<pre>}<\/pre>\n<p>I know too that it is possible to get the locale from within the service via the application context, but that too seems ill advised if a way around it, so I&#8217;m trying to stick to the most &#8220;orthodox&#8221; approach I can come up with.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Seems (to me) like an obvious need &#8211; referencing a translatable message in a service in a Grails app. But you can&#8217;t just use g:message &#8211; well, I&#8217;ve seen some workarounds that let you access the g namespace, but that doesn&#8217;t really seem advisable. I just want message handling. I found several references to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,3],"tags":[],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-grails","category-programming-and-web-design"],"_links":{"self":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":1,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":63,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts\/62\/revisions\/63"}],"wp:attachment":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}