如何在Grails中使用控制器外的g.message?

Top*_*era 3 tags grails controller internationalization

可能重复:
Grails - 从控制器获取消息值

我想在类中使用g.message()方法Service,而不是Controller.但我不能.

代码示例:

// this is a method OUTSIDE a Controller 
def show = {
    def msg = message(code:"foo.bar") // I'm getting error here
}
Run Code Online (Sandbox Code Playgroud)

有可能这样做,或者我必须使用旧ResourceBundle班级?

Top*_*era 11

另一个问题有一个完美的答案:

import org.springframework.context.i18n.LocaleContextHolder as LCH
...
class MyServiceOrMyDomain {
  def messageSource 
  ...
   messageSource.getMessage(key, ["myArg"].toArray(), LCH.getLocale())
  ...
}
Run Code Online (Sandbox Code Playgroud)

感谢fabien7474