12 Replies Latest reply on Aug 6, 2007 10:19 AM by knaas

    howto display the an enum object internationalized

    hasc

      Hi,

      i have some trouble displaying an enum item internationalized. i look for something like that:


      <h:outputText value="#{messages[enumObject]}"/>
      
      or
      
      <h:outputText value="#{messages[enumObject]}">
       <s:convertEnum/>
      </outputText>


      while

      enumObject is a valueBinding to a corresponing getter method which returns an enum object.

      both ways didn't work for me. does anyone know a way to realize it?

      thanks an regards,
      hasc



        • 1. Re: howto display the an enum object internationalized
          smurfs

          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

            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

              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

                sorry, "rather the key for the label in the the model."

                • 5. Re: howto display the an enum object internationalized
                  hasc

                  i tried it an it works fine.

                  thanks

                  • 6. Re: howto display the an enum object internationalized
                    nickarls

                    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

                      Post your code :)

                      • 8. Re: howto display the an enum object internationalized
                        nickarls

                         

                        "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

                          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

                            Sometimes it feels like it.

                            so orderBean.filter is a String? It should be an Enum

                            FilterType filter;


                            • 11. Re: howto display the an enum object internationalized
                              lcoetzee

                              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

                                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;
                                 }