Grails:如何从控制器或服务访问i18n?
我制作了这个控制器,应该用来发送电子邮件。我需要访问Grails:如何从控制器或服务访问i18n?,grails,Grails,我制作了这个控制器,应该用来发送电子邮件。我需要访问i18n才能发送本地化电子邮件 class MailController { MessageSource messageSource static transactional = false public void sendEmail() { String name = "some name..." String subject = message(code:"somemessag
i18n
才能发送本地化电子邮件
class MailController {
MessageSource messageSource
static transactional = false
public void sendEmail() {
String name = "some name..."
String subject = message(code:"somemessagekey", args:[name])
// do some fancy stuff here...
}
}
然后有i18n
文件(位于i18n
文件夹中):
在我运行这个之后,它抛出(在集成测试中):
groovy.lang.MissingPropertyException:没有这样的属性:messageSource
对于类:org.codehaus.groovy.grails.support.MockApplicationContext
我不知道如何在controller中处理该本地化(我也在服务中尝试过,但这更复杂)。如果您使用的是grails 1.3.7,则需要将消息源声明为:
def messageSource
这样它就会被注入到你的控制器中。我认为在2.0中,您可以按照发布的方式进行操作,但即便如此,我认为还是值得尝试如上所述进行声明。在Grails 2.2.3中,它应该可以:
import org.springframework.context.MessageSource
...
def MessageSource messageSource
...
String subject = messageSource.getMessage("somemessagekey", [name], null)
...
参考链接和更多可能性:我知道有答案,但这里是我的看法。我做了一项服务(受他人启发) 现在,在服务中,只需注入i18n服务,您就可以开始了!这也使得其他服务的单元测试变得轻而易举,因为您可以模拟i18n服务
作为奖励,该服务将尝试查找当前webrequest的当前区域设置,前提是该服务在webrequest上下文中运行。上面的回答对我没有帮助。 我正在使用Grails3.0.1,下面的解决方案对我很有用
import org.springframework.context.i18n.LocaleContextHolder as LCH
import grails.util.Holders;
class MyService{
def messageSource = Holders.getGrailsApplication().getParentContext();
public String MyMethod()
{
String message = messageSource.getMessage("code",args,LCH.getLocale());
}
}
官方的方法其实很简单。声明如下: 在控制器中读取消息很简单,因为您可以调用标记 作为方法:
def show() {
def msg = message(code: "my.localized.content", args: ['Juan', 'lunes'])
}
在中也可以使用相同的技术,但是如果您的标记
库使用自定义的调用,则必须为调用添加前缀
使用g.
:
def myTag = { attrs, body ->
def msg = g.message(code: "my.localized.content", args: ['Juan', 'lunes'])
}
另外,请注意,当您的翻译不使用参数时,不要提供参数:
def msg = message(code: "my.localized.content")
我使用的是grails 3.x.Injected
messageSource
org.springframework.context.MessageSource
使用
String getMessage(String code, Object[] args, String defaultMessage, Locale locale);
此方法无效。请将您的代码修改为
class MailController {
MessageSource messageSource
static transactional = false
public void sendEmail() {
String name = "some name..."
String subject = messageSource.getMessage('somemessagekey', [name], '', null)
// do some fancy stuff here...
}
}
我尝试了下面的代码和它的工作使用持有人
def messageSource = Holders.getGrailsApplication().getMainContext().getBean("messageSource")
String errorMesssage = messageSource.getMessage('validation.error.catalogue.not.found', null, messageSource.getMessage(
'validation.error.catalogue.not.found', null, 'No Content found for the Reserved Instance Plan Catalogue', locale), locale)
你不需要调用g.message()吗?我为什么要这样做?我没有找到关于“g”的信息。是的,这很有帮助,谢谢。工作代码是:
def messageSource public void sendmail(){String subject=message(代码:“somemessagekey”,参数:[name])…
def MessageSource不应该是MessageSource MessageSource还是def MessageSource?声明def和类是冗余的。如果需要从另一个控制器调用控制器的方法,则MessageSource将为null。(至少对我而言)。所以,您自己设置它可能会很有趣。此答案仅对控制器有效。在服务中,它不会以这种方式工作。
class MailController {
MessageSource messageSource
static transactional = false
public void sendEmail() {
String name = "some name..."
String subject = messageSource.getMessage('somemessagekey', [name], '', null)
// do some fancy stuff here...
}
}
def messageSource = Holders.getGrailsApplication().getMainContext().getBean("messageSource")
String errorMesssage = messageSource.getMessage('validation.error.catalogue.not.found', null, messageSource.getMessage(
'validation.error.catalogue.not.found', null, 'No Content found for the Reserved Instance Plan Catalogue', locale), locale)