如何在g中使用带有Grails / Groovy枚举的i18n:select? [英] How to use i18n with a Grails/Groovy enum in a g:select?

查看:122
本文介绍了如何在g中使用带有Grails / Groovy枚举的i18n:select?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用i18n本地化工作在Grails / Groovy枚举上,

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"/>


推荐答案

对不起,我认为这可以帮助您。我对enums和i18n有着完全相同的问题。这是我找到的解决方案:

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:

在您之前定义的枚举之后,在您的message.properties文件中为每个枚举值输入一个条目,例如:

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

然后,当您想要在select元素中显示enum的值时,请按照以下操作:

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"/>

根据关于select标签的Grails文档,使用valueMessagePrefix属性中的值由一个点(。),然后是枚举元素的值。这样它就会进入message.properties文件并搜索你之前放置的行。

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!

这篇关于如何在g中使用带有Grails / Groovy枚举的i18n:select?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆