Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: i18n with enum

  1. #11
    Join Date
    Nov 2009
    Posts
    8

    Default Alternate Solution

    I found another solution which allows the conversion to happen in the enum, as I use them from a number of controllers, and have a lot of enums too so the converter method isn't practical.

    I defined a bean in webmvc-config.xml

    Code:
    <bean id="contextApplicationContextProvider" class="ApplicationContextProvider"></bean>
    To inject the application context into this class which sets it in AppContext

    Code:
    public class ApplicationContextProvider implements ApplicationContextAware { 
        public void setApplicationContext(ApplicationContext ctx) throws BeansException { 
    
            // Wiring the ApplicationContext into a static method 
            AppContext.setApplicationContext(ctx); 
        } 
    }
    Code:
    public class AppContext { 
    
        private static ApplicationContext ctx; 
    
        /**
         * Injected from the class "ApplicationContextProvider" which is automatically
         * loaded during Spring-Initialization.
         */ 
    
        public static void setApplicationContext(ApplicationContext applicationContext) { 
            ctx = applicationContext; 
        } 
    
    
        /**
         * Get access to the Spring ApplicationContext from everywhere in your Application
         *
         * @return
         */ 
    
        public static ApplicationContext getApplicationContext() { 
            return ctx; 
        } 
    }
    Then in the enum:

    Code:
    public enum MyEnum {
    	VALUEONE("valueOne.text"),
    	VALUETWO("valueTwo.text");
    	
    	MyEnum(String s) { label = s; }
        
        String label;
        
        public String getLabel() { 
        	Locale locale = LocaleContextHolder.getLocale();
        	return AppContext.getApplicationContext().getMessage(label, null, locale);
        }
    
        public String toString()
        {
        	return getLabel();
        }
    }

  2. #12
    Join Date
    Aug 2009
    Location
    Montréal
    Posts
    23

    Default

    Is't working for you from the list screen.
    It works for me when creating a new object, but when I try to display all the objects it shows the technical value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •