-
1. Re: howto display the an enum object internationalized
smurfs Jun 11, 2007 5:17 PM (in response to hasc)This is an example of an approach I have used for drop-down lists. Each constant is instantiated with the key defined in the messages properties file:
public enum Salutation { MR("salutation-mr-label"), MRS("salutation-mrs-label"), MS("salutation-ms-label"); private String labelKey; Salutation(String labelKey) { this.labelKey = labelKey; } public String getLabelKey() { return labelKey; } }
Need to create a factory to return array of enum values to view@Name("factories") public class Factories { @org.jboss.seam.annotations.Factory("salutationTypes") public Salutation[] getSalutationTypes() { return Salutation.values(); } }
<f:selectOneMenu id="salutations" value="#{backingBean.salutation}"> <s:selectItems value="#{salutationTypes}" var="salutation" label="#{messages[salutation.labelKey]}"/> <s:convertEnum /> </f:selectOneMenu>
I appreciate this doesn't answer your query directly but it may give you a few clues. -
2. Re: howto display the an enum object internationalized
hasc Jun 12, 2007 6:45 AM (in response to hasc)thanks for your reply.
this approach may be a solution but i would appreciate a way where i don't have to "carry" the label in the model. i defined the enumeration like that:public enum MyEnum { VALUE1, VALUE2, VALUE3 }
now i m looking for a solution where i can display the label internationalized. But if theres no way i will take your solution. so thanks... -
3. Re: howto display the an enum object internationalized
pmuir Jun 12, 2007 6:53 AM (in response to hasc)Smurfs isn't putting the label in the model, rather the key to the model. Using jboss-el you could do
<f:selectOneMenu value="#{backingBean.salutation}"> <s:selectItems value="#{salutations}" var="salutation" label="#{messages[salutation.name()]}"/> <s:convertEnum /> </f:selectOneMenu>
with the factory as Smurfs said. You then get have to provide keys for VALUE1, VALUE2 etc. -
4. Re: howto display the an enum object internationalized
pmuir Jun 12, 2007 6:54 AM (in response to hasc)sorry, "rather the key for the label in the the model."
-
5. Re: howto display the an enum object internationalized
hasc Jun 12, 2007 7:04 AM (in response to hasc)i tried it an it works fine.
thanks -
6. Re: howto display the an enum object internationalized
nickarls Aug 3, 2007 8:15 AM (in response to hasc)Hi,
What could be the problem when your example works "one-way". I see the labels fine but when I it goes back into the model I get an IllegalArgumentException. Just like it would be trying to put the entire enumeration back into the bound string.
The documentation is not that verbose on the use of ... -
7. Re: howto display the an enum object internationalized
pmuir Aug 3, 2007 8:21 AM (in response to hasc)Post your code :)
-
8. Re: howto display the an enum object internationalized
nickarls Aug 3, 2007 8:34 AM (in response to hasc)"pete.muir@jboss.org" wrote:
Post your code :)public enum FilterType { A("orders.filtering.all"), O("orders.filtering.open"), C("orders.filtering.closed"); private String key Suodatustyyppi(String key) { this.key = key } public String getKey() { return key; } }
<ice:selectOneRadio value="#{orderBean.filter}"> <s:selectItems value="#{orderBean.filterTypes}" var="filter" label="#{messages[filter.key]}"/> <s:convertEnum /> </ice:selectOneRadio>
PS. Do you work full-time answering questions here? :) Much appreciated for a fresh seam-convert in any case... -
9. Re: howto display the an enum object internationalized
nickarls Aug 3, 2007 8:37 AM (in response to hasc)ngh, missed on the preview/submit
orderBean: @Factory("filterTypes") public FilterType[] getFilterTypes() { return FilterType.values(); } The radio button would be pointing into a String
-
10. Re: howto display the an enum object internationalized
pmuir Aug 3, 2007 8:50 AM (in response to hasc)Sometimes it feels like it.
so orderBean.filter is a String? It should be an EnumFilterType filter;
-
11. Re: howto display the an enum object internationalized
lcoetzee Aug 3, 2007 10:10 AM (in response to hasc)What we do is the following:
public enum GenderEnum { FEMALE, MALE; public final static String translatedGenders[][] = { {"Female", "Male"}, {"Vroulik", "Manlik"}, {"Female - Zulu", "Male -Zulu"}, {"Female - ndebele", "Male - ndebele"}, {"Female - xhosa", "Male - xhosa"}, {"Female - sotho", "Male - sotho"}, {"Mosadi", "Monna"}, {"Female - setswana", "Male - setswana"}, {"Female - siswati", "Male - siswati"}, {"Female - venda", "Male - venda"}, {"Female - songa", "Male - songa"} }; }
Which is then accessed (in Java or JSF)GenderEnum.translatedGenders[userEnvironment.getLanguage().ordinal()] [GenderEnum.MALE.ordinal()]
If you want to get a specific translation for a specific enum.
Regards
Louis -
12. Re: howto display the an enum object internationalized
knaas Aug 6, 2007 10:19 AM (in response to hasc)What we did was to create an EnumConverter that uses the resource bundle for internationalization. We just defined a convention "packagename.classname.enumvalue=Some text".
If we find something in the resource bundle, use it, otherwise just use the toString value of the enum.
Something like this code.@Override public String getAsString(final FacesContext context, final UIComponent component, final Object object) throws ConverterException { String retVal = null; if (object instanceof Enum) { Enum instance = (Enum)object; final String resourceKey = instance.getClass().getName() + "." + instance.name(); try { retVal = ResourceBundle.instance().getString(resourceKey); } catch (MissingResourceException e) { //Ignore } if (StringUtils.isEmpty(retVal)) { retVal = instance.name(); } } return retVal; }