Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Localizing Enums
Localizing Enums [message #893019] Mon, 02 July 2012 10:26 Go to next message
Alexey Romanov is currently offline Alexey RomanovFriend
Messages: 263
Registered: May 2010
Senior Member
While NLS class is nice to use, I sometimes want to localize messages corresponding to enums. Currently it looks like this in Java code:
package foo.bar;
public enum YesNo { YES, NO }

package foo.bar;
public class Msg extends NLS {
  public String yes;
  public String no;
  ... // other string fields
  
  static { /* init messages */ }

  public String getYesNoMsg(YesNo arg) {
    switch(arg) {
      case YES: return yes;
      case NO: return no;
    }
  }
}

and in properties file:
yes = ...
no = ...

So changing enum means I need to change 4 places:


  1. Enum itself
  2. Fields of Msg
  3. getYesNoMsg method
  4. properties file


Obviously, I can't get away from having to change 1 and 4, but ideally those should be the only places which have to change:
public enum YesNo { YES, NO }
package foo.bar;
public enum YesNo { YES, NO }

package foo.bar;
public class Msg extends NLS {
  public EnumMap<YesNo, String> yesNoMap;
  
  static { /* init messages */ }

  public String getYesNoMsg(YesNo arg) {
    return yesNoMap.get(arg)
  }
}

and in properties file:
foo.bar.YesNo.YES = ...
foo.bar.YesNo.NO = ...


The best I can do currently is 3 places to change: keep yes and no fields (renamed to foo_bar_YesNo_YES/NO), and fill EnumMap by reflection after initializing messages. Is there a better way that I missed?
Re: Localizing Enums [message #894432 is a reply to message #893019] Mon, 09 July 2012 08:42 Go to previous message
Alexey Romanov is currently offline Alexey RomanovFriend
Messages: 263
Registered: May 2010
Senior Member
I ended up with a pretty simple solution:
package ru.focusmedia.rcplibs.rcputil;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.osgi.util.NLS;

public abstract class NLSEnum extends NLS {
	private static Map<Class<?>, Map<Enum<?>, String>> classesToEnumMessages;

	public static String getEnumText(Enum<?> enumValue, Class<?> nlsClass) {
		return getEnumText(enumValue, nlsClass, enumValue.getDeclaringClass().getSimpleName());
	}

	public static String getEnumText(Enum<?> enumValue, Class<?> nlsClass, String prefix) {
		Map<Enum<?>, String> enumMessages = classesToEnumMessages.get(nlsClass);
		if (enumMessages == null) {
			enumMessages = new HashMap<Enum<?>, String>();
			classesToEnumMessages.put(nlsClass, enumMessages);
		}
		
		String storedResult = enumMessages.get(enumValue);
		if (storedResult != null) {
			return storedResult;
		} else {
			String result;
			try {
				String fieldName = prefix + "_"
						+ enumValue.name();
				Field field = nlsClass.getField(fieldName);
				result = (String) field.get(null);
			} catch (Exception e) {
				result = (enumValue.name().charAt(0) + enumValue.name().substring(1).toLowerCase()).replace('_', ' ');
			}
			enumMessages.put(enumValue, result);
			return result;
		}
	}
}

In extending class:
public class Msg extends NLSEnum {

    public String getYesNoMsg(YesNo arg) {
        return getEnumText(arg, Msg.class);
    }
}
Previous Topic:WorkbenchSourceProvider
Next Topic:app doesn't open any view nor editor after update to juno
Goto Forum:
  


Current Time: Tue Mar 19 03:22:16 GMT 2024

Powered by FUDForum. Page generated in 0.01767 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top