Hi PetrWe use a custom EnumConverter for that. It can be added to the application like so:
@Override
protected IConverterLocator newConverterLocator () {
ConverterLocator converterLocator = (ConverterLocator)
super.newConverterLocator();
converterLocator.set(MyEnum.class, new EnumConverter());
}
EnumConverter:
...
public String convertToString (Object value, Locale locale) {
return EnumUtils.getEnumFromResources((Enum<?>) value,
value.toString());
} ...Then the following helper method looks the enum up in the string resources...
/**
* Tries to resolve a given Enum instance against the resources.
* The following keys are searched for in the resources:<br/>
* <b><i>If the enum is defined as a top-level enum:</i></b>
* <ul>
* <li>package.EnumClassName.ENUMNAME</li>
* <li>EnumClassName.ENUMNAME</li>
* </ul>
* <b><i>If the enum is defined inside another class:</i></b>
* <ul>
* <li>package.SomeClass$EnumClassName.ENUMNAME</li>
* <li>package.SomeClass.EnumClassName.ENUMNAME</li>
* <li>SomeClass.EnumClassName.ENUMNAME</li>
* <li>EnumClassName.ENUMNAME</li>
* </ul>
* If no resource could be found, the specified defaultValue is
returned.
*
* @param enu The Enum instance
* @param defaultValue The default value in case no resource string
was found
* @return The matching resource string or <tt>defaultValue</tt> if
there was no match
*/
public static String getEnumFromResources (Enum<?> enu, String
defaultValue) {
final Localizer loc = WebApplication.get().getResourceSettings().getLocalizer();
List<String> keyList = new ArrayList<String>();
keyList.add(enu.getClass().getName());
keyList.add(enu.getClass().getCanonicalName());
if (enu.getClass().getDeclaringClass() != null) {
keyList.add(enu.getClass().getDeclaringClass().getSimpleName()+"."+enu.getClass().getSimpleName());
}
keyList.add(enu.getClass().getSimpleName());
for (String key : keyList) {
// The empty string needs to be used here because null is
being treated as 'not found',
// thus might result in a warning string being returned.
String res = loc.getString(key+"."+enu.name(), null, "");
if (!"".equals(res)) {
return res;
}
}
return defaultValue;
}
Petr Fejfar wrote:
Hi all,
I have an enum type e.g.
public enum MapType
UNDEFINED,ROADMAP,MOBILE,SATELITE,TERRAIN,HYBRID;
public static String toKeyword(MapType mapType) {
switch (mapType) {
case ROADMAP:
return "roadmap";
...
public static MapType fromKeyword(final String keyword) {
final String kw = keyword.toLowerCase();
if (kw.equals("roadmap"))
return ROADMAP;
else if ...
and I'd like to localize a verbal representation of particular elements
which I could share throughout Wicket application in many places.
i.e. I need something like this:
public static String verbose(MapType mapType) {
switch (mapType) {
case ROADMAP:
return getString("verbROADMAP");
...
What is a preferred/recommended way to localize it?
Thanks, Petr
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
smime.p7s
Description: S/MIME Cryptographic Signature
