Niall McLoughlin wrote:
I hope this is the right place to ask this question.
Let's say I have the following class (using jdk 1.5): public enum UserType {
     MANAGER,
     SUPERVISOR,
     USER;
 }

 I can build a List of these types to display in an options tag easily enough:
public List<ValueLabelPair> getUserTypes()
     throws Exception {
     List<ValueLabelPair> items = new ArrayList< ValueLabelPair>();
     for( UserType userType : UserType.values()) {
         items.add(new ValueLabelPair( String.valueOf(
userType.ordinal(), userType.name() ) ));
     }
     return items;
 }
This works well, but what happens when I need to 'internationalize'
my app for other languages or want to display another 'name' instead
of the enum name without messing with my model? What would be the best
way to convert those display names if I stored the labels in a
MessageResources file?

The way I generally handle this would be to treat UserType.name as a resource key. Then in my JSP I'd display the enum value like this:

  <bean:message key-"${types.name}"/>

or, more generally,

  <bean:message key-"UserType.label.${types.name}"/>

By doing the localization at the top level like this, rather than in code, I can treat the enum values the same way wherever they're used in the code and not have to worry about passing in a locale object where it otherwise wouldn't be needed.

L.
--
Laurie Harper
Open Source advocate, Java geek: http://www.holoweb.net/laurie
Founder, Zotech Software: http://www.zotechsoftware.com/


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to