如何在 ag:select 中将 i18n 与 Grails/Groovy 枚举一起使用?
我正在尝试在 Grails/Groovy 枚举上进行 i18n 本地化,
public enum Notification {
GENERIC(0),
CONFIRM_RESERVATION(100),
CONFIRM_ORDER(200),
CONFIRM_PAYMENT(300),
final int id;
private Notification(int id) {
this.id = id
}
String toString() {
id.toString()
}
String getKey() {
name()
}
}
有什么关于我如何实现这一目标的提示吗?我尝试将完整的类名等放入本地化中,但这似乎不起作用
<g:select from="${Notification.values()}" name="notification" valueMessagePrefix="full.path.to.package.Notification"/>
i am trying to get i18n localisation working on an Grails/Groovy enum,
public enum Notification {
GENERIC(0),
CONFIRM_RESERVATION(100),
CONFIRM_ORDER(200),
CONFIRM_PAYMENT(300),
final int id;
private Notification(int id) {
this.id = id
}
String toString() {
id.toString()
}
String getKey() {
name()
}
}
Any hints on how i could achieve that? I tried to put the full classname etc in a localisation but this does noet seem to work
<g:select from="${Notification.values()}" name="notification" valueMessagePrefix="full.path.to.package.Notification"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抱歉耽搁了,但我认为这可以帮助你。我在枚举和 i18n 方面遇到了完全相同的问题。这是我找到的解决方案:
按照之前定义的枚举,在 message.properties 文件中为枚举的每个值添加一个条目,例如:
然后,当您想在选择元素中显示枚举的值时,请执行以下操作:
根据有关 select 标记的 Grails 文档,您在属性 valueMessagePrefix 中放入的值将使用后跟一个点 (.),然后是枚举元素的值。这样它就会转到 message.properties 文件并搜索您之前放置的行。
现在,您需要做的另一件事是例如在数据列表中,显示每个记录的枚举值。在这种情况下,您需要执行以下操作:
这是如果您有一个具有通知类型属性的域类。
希望这有帮助。
再见!
Sorry for the delay but I think this could help you. I was having the exact same problem with enums and i18n. This is the solution I found:
Following your enum defined before, in your message.properties files put an entry for each value of the enum for example:
Then when you want to show the values of the enum in a select element then do as follows:
According to the Grails documentation regarding the select tag, the value you put in the attribute valueMessagePrefix is used followed by a dot(.) and then the value of the element of the enum. So that way it would go to the message.properties file and search for the lines you put before.
Now, the other thing you would need to do is for example in a list of data, show the value of the enum for each record. In that case you need to do as follows:
This is if you have a domain class with one attribute of type Notification.
Hope this helped.
Bye!
Rob Fletcher 的这篇博文中展示了一种方法(从 2009 年开始)
确保您的枚举类
实现 org.springframework.context.MessageSourceResolvable
然后实现 它定义的方法
One method is shown in this blog post by Rob Fletcher (from 2009)
Make sure your enum class
implements org.springframework.context.MessageSourceResolvable
Then implement the methods it defines
您需要实现
MessageSourceResolvable
来提供您的代码:并在 i18n 中定义您的消息:
select 标签应如下所示:
You need to implement
MessageSourceResolvable
to provide your codes:And define your messages in i18n:
The select tag should look like this: