How to use i18n with a Grails/Groovy enum in a g:select?
Asked Answered
C

3

8

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"/>
Chasidychasing answered 27/9, 2011 at 15:36 Comment(0)
T
13

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!

Tappet answered 6/10, 2011 at 13:14 Comment(1)
You deserve a +100 but since I couldn't do so, I did +1! Thanks.Hagy
K
7

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

Koffler answered 27/9, 2011 at 15:45 Comment(0)
F
0

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"/>
Fiddlestick answered 20/1, 2020 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.