|
The tag function message can be used in a controller thus:
message(code:"xxxxxx"); To internationalize a log message I want to use the message function in a service. What do I have to include to do this? /inge --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
def messageSource messageSource.getMessage(java.lang.String code, java.lang.Object[] args, java.util.Locale locale) |
|
In reply to this post by inge
This may help :
http://grails.1312388.n4.nabble.com/I18n-for-domain-class-service-td1333616.html
On Tue, Jan 17, 2012 at 4:28 PM, inge <[hidden email]> wrote: The tag function message can be used in a controller thus: Regards Gaurav Chauhan Contact No : +91-95607-66664 Skype : chauhan.gaurav Website : JellyFish Technologies
|
|
In reply to this post by inge
On 17/01/2012 10:58, inge wrote:
> The tag function message can be used in a controller thus: > message(code:"xxxxxx"); > > To internationalize a log message I want to use the message function in > a service. What do I have to include to do this? Inject the messageSource Spring bean into your service and use that to render the messages, something like: import org.springframework.context.i18n.LocaleContextHolder class MyService { def messageSource void logSomething(code) { log.info(messageSource.getMessage(code, LocaleContextHolder.locale)) } } Using the LocaleContextHolder should give you the locale of the current request if there is one or use the default locale on the server if called from outside a controller action. If you always want to log using the server's language rather than the user's language then use a Locale parameter of null. Ian -- Ian Roberts | Department of Computer Science [hidden email] | University of Sheffield, UK --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
Thanks for the help. I should have said in my first post that I need to
send args argument to message thus: message(code:"xxxx" args:[Y, Z]); I would like something like this: void logSomething(code, args) { log.info(messageSource.getMessage(code, LocaleContextHolder.locale)) } How do I send the args argument to messageSource.getMessage /inge 12 12:23 PM, Ian Roberts wrote: > On 17/01/2012 10:58, inge wrote: >> The tag function message can be used in a controller thus: >> message(code:"xxxxxx"); >> >> To internationalize a log message I want to use the message function in >> a service. What do I have to include to do this? > Inject the messageSource Spring bean into your service and use that to > render the messages, something like: > > import org.springframework.context.i18n.LocaleContextHolder > > class MyService { > def messageSource > > void logSomething(code) { > log.info(messageSource.getMessage(code, LocaleContextHolder.locale)) > } > } > > Using the LocaleContextHolder should give you the locale of the current > request if there is one or use the default locale on the server if > called from outside a controller action. If you always want to log > using the server's language rather than the user's language then use a > Locale parameter of null. > > Ian > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
google the javadocs for org.springframework.context.MessageSource. thats what the bean implements
getMessage(code, args (as []), locale) On Jan 18, 2012, at 3:27 AM, inge wrote: > Thanks for the help. I should have said in my first post that I need to send args argument to message thus: > message(code:"xxxx" args:[Y, Z]); > > I would like something like this: > > void logSomething(code, args) { > log.info(messageSource.getMessage(code, LocaleContextHolder.locale)) > } > > How do I send the args argument to > > messageSource.getMessage > > /inge > > 12 12:23 PM, Ian Roberts wrote: >> On 17/01/2012 10:58, inge wrote: >>> The tag function message can be used in a controller thus: >>> message(code:"xxxxxx"); >>> >>> To internationalize a log message I want to use the message function in >>> a service. What do I have to include to do this? >> Inject the messageSource Spring bean into your service and use that to >> render the messages, something like: >> >> import org.springframework.context.i18n.LocaleContextHolder >> >> class MyService { >> def messageSource >> >> void logSomething(code) { >> log.info(messageSource.getMessage(code, LocaleContextHolder.locale)) >> } >> } >> >> Using the LocaleContextHolder should give you the locale of the current >> request if there is one or use the default locale on the server if >> called from outside a controller action. If you always want to log >> using the server's language rather than the user's language then use a >> Locale parameter of null. >> >> Ian >> > > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
I've created this helpe service which I use in my project when I need to get to i18n messages in controller, services, etc.
class I18nService { boolean transactional = false SessionLocaleResolver localeResolver MessageSource messageSource /** * * @param msgKey * @param defaultMessage default message to use if none is defined in the message source * @param objs objects for use in the message * @return */ def msg(String msgKey, String defaultMessage = null, List objs = null) { def msg = messageSource.getMessage( msgKey, objs == null ? null : objs.toArray(), defaultMessage, localeResolver.defaultLocale) if(msg == null || msg == defaultMessage){ log.warn("No i18n messages specified for msgKey: ${msgKey}") msg = defaultMessage } return msg } } On 18 January 2012 09:42, Josh (basejump) <[hidden email]> wrote: google the javadocs for org.springframework.context.MessageSource. thats what the bean implements |
|
I actually copied some of the work done on the localizations plugin
(http://grails.org/plugin/localizations) but only used it to add a message method to the services metaClasses. The plugin adds things to For example, I have a static class and method called MessageUtility and getMessage available in src/groovy: (Disclaimer - copied directly from the localizations plugin, I just didn't want the persisted messages stuff!) static String getMessage(parameters) { def requestAttributes = RequestContextHolder.getRequestAttributes() def applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext Holder.getServletContext()) boolean unbindRequest = false // Outside of an executing request, establish a mock version if (!requestAttributes) { requestAttributes = GrailsWebUtil.bindMockWebRequest(applicationContext) unbindRequest = true } def messageSource = applicationContext.getBean("messageSource") def locale = RequestContextUtils.getLocale(requestAttributes.request) // What the heck is going on here with RequestContextUtils.getLocale() returning a String? // Don't know why - just fix it! if (locale instanceof String) { // Now Javasoft have lost the plot and you can't easily get from a Locale.toString() back to a locale. Aaaargh! if (locale.length() >= 5) { locale = new Locale(locale[0..1], locale[3..4]) } else { locale = new Locale(locale) } } def msg if (parameters.error) msg = messageSource.getMessage(parameters.error, locale) else msg = messageSource.getMessage(parameters.code, parameters.args as Object[], parameters.default, locale) if (unbindRequest) RequestContextHolder.setRequestAttributes(null) if (parameters.encodeAs) { switch (parameters.encodeAs.toLowerCase()) { case 'html': msg = msg.encodeAsHTML() break case 'xml': msg = msg.encodeAsXML() break case 'url': msg = msg.encodeAsURL() break case 'javascript': msg = msg.encodeAsJavaScript() break case 'base64': msg = msg.encodeAsBase64() break } } return msg } NOTE: I modified the method to return correctly if error is set. I then inject the message method into all services in the BootStrap - note that this is better done in a plugin due to the fact that it is not injected when a class is changed in development dynamically. grailsApplication.serviceClasses.each { serviceClass -> serviceClass.metaClass.message = { Map map -> MessageUtility.getMessage(map) } } And voila! You can do message(code:"some.code", default:"default message") in any service. Much easier and consistent with how things are done in controllers. Maybe someday I'll break this off into its own plugin to handle the service reloading correctly. -Brian --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Powered by Nabble | Edit this page |
