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

前端 未结 3 1720
谎友^
谎友^ 2021-02-04 16:10

i am trying to get i18n localisation working on an Grails/Groovy enum,

public enum Notification  {
    GENERIC(0),
    CONFIRM_RESERVATION(100),
    CONFIRM_ORDE         


        
3条回答
  •  旧巷少年郎
    2021-02-04 16:47

    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:

    
    

提交回复
热议问题