如何在 ag:select 中将 i18n 与 Grails/Groovy 枚举一起使用?

发布于 2024-12-06 22:26:10 字数 579 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒解孤独 2024-12-13 22:26:11

抱歉耽搁了,但我认为这可以帮助你。我在枚举和 i18n 方面遇到了完全相同的问题。这是我找到的解决方案:

按照之前定义的枚举,在 message.properties 文件中为枚举的每个值添加一个条目,例如:

enum.value.GENERIC
enum.value.CONFIRM_RESERVATION
enum.value.CONFIRM_ORDER
enum.value.CONFIRM_PAYMENT

然后,当您想在选择元素中显示枚举的值时,请执行以下操作:

<g:select from="${path.to.package.Notification.values()}"  keys="${path.to.package.Notification?.values()}" name="notification" valueMessagePrefix="enum.value"/>

根据有关 select 标记的 Grails 文档,您在属性 valueMessagePrefix 中放入的值将使用后跟一个点 (.),然后是枚举元素的值。这样它就会转到 message.properties 文件并搜索您之前放置的行。

现在,您需要做的另一件事是例如在数据列表中,显示每个记录的枚举值。在这种情况下,您需要执行以下操作:

${message(code:'enum.value.'+fieldValue(bean: someDomainClass, field: "notification"))}

这是如果您有一个具有通知类型属性的域类。

希望这有帮助。
再见!

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:

enum.value.GENERIC
enum.value.CONFIRM_RESERVATION
enum.value.CONFIRM_ORDER
enum.value.CONFIRM_PAYMENT

Then when you want to show the values of the enum in a select element then do as follows:

<g:select from="${path.to.package.Notification.values()}"  keys="${path.to.package.Notification?.values()}" name="notification" valueMessagePrefix="enum.value"/>

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:

${message(code:'enum.value.'+fieldValue(bean: someDomainClass, field: "notification"))}

This is if you have a domain class with one attribute of type Notification.

Hope this helped.
Bye!

以歌曲疗慰 2024-12-13 22:26:11

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

江湖正好 2024-12-13 22:26:11

您需要实现 MessageSourceResolvable 来提供您的代码:

enum Notification implements org.springframework.context.MessageSourceResolvable {

    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()
    }

    public Object[] getArguments() { [] as Object[] }

    //This methods do the trick
    public String[] getCodes() { [ "notification." + name() ] }

    public String getDefaultMessage() { name() }
}

并在 i18n 中定义您的消息:

notification.GENERIC=Generic
notification.CONFIRM_RESERVATION=Confirm reservation
notification.CONFIRM_ORDER=Confirm order
notification.CONFIRM_PAYMENT=Confirm payment

select 标签应如下所示:

<g:select name="type" from='${Notification.values()}' optionKey="id"/>

You need to implement MessageSourceResolvable to provide your codes:

enum Notification implements org.springframework.context.MessageSourceResolvable {

    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()
    }

    public Object[] getArguments() { [] as Object[] }

    //This methods do the trick
    public String[] getCodes() { [ "notification." + name() ] }

    public String getDefaultMessage() { name() }
}

And define your messages in i18n:

notification.GENERIC=Generic
notification.CONFIRM_RESERVATION=Confirm reservation
notification.CONFIRM_ORDER=Confirm order
notification.CONFIRM_PAYMENT=Confirm payment

The select tag should look like this:

<g:select name="type" from='${Notification.values()}' optionKey="id"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文