5 Replies Latest reply on Mar 13, 2008 3:47 PM by wark2007

    SelectItems, Hibernate and i18n

      Hello everybody! I have the following problem and hope that someone could give me some help and/or advices.


      For example I have the following entity and the enum:


      public class Person {
      
        @Enumerated(EnumType.ORDINAL)
        @Column(name="sex")
        Sex sex = Sex.MALE;
        
        //getter and setter
      }
      
      public enum Sex {
        MALE, FEMALE;
      }
      



      My problem is, if the user creates or updates an instance of type Person he can select Male or Female from a selectOneList. In a dataTable I will just show Male or Female in one column. Till now everything works fine. But I will show the translations for Male and Female regarding to the user locale.


      Simple MessageResource couldn't help me, since the content is dynamic. There is just one approach in my mind: Using converters. But that seems not very smart to me, since I need at least two converters for this little example. One for the selectOneList and one for h:outputText in the datatable.


      I'am a sure that this is a first class problem and there must be a clean solution. How do you deal with that kind of problem?


      Thanks in advance!

        • 1. Re: SelectItems, Hibernate and i18n
          nickarls

          Extend your enum to have a String label that contains a key to a resource bundle, the you can use convertEnum and reference the key in the selectbox label.

          • 2. Re: SelectItems, Hibernate and i18n

            Thanks for your reply!


            You mean something like this:


            public enum Sex {
              MALE, FEMALE;
            
              public getLabel() {
            
                Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale()
                ResourceBundle bundle = BundleRepository.getBundle(locale);
            
                switch (this) {
                  case MALE: return bundle.getString("male");
                  default: return bundle.getString("female");
                }
              }
            }
            



            That looks good, but has the disadvantage that your enum is strongly coupled with your UI. Or am I missing something important?


            Thanks!

            • 3. Re: SelectItems, Hibernate and i18n
              nickarls
              
              public enum Sex {
              
                   MALE("some.key.male"),
              
                   FEMALE("some.key.female")
              
              
                   private String key;
              
                        
              
                   Sex(String key) {
              
                        this.key= key;
              
                   }
              
                   
              
                   public String getKey() {
              
                        return key;
              
                   }
              
              }
              
              

              • 4. Re: SelectItems, Hibernate and i18n
                nickarls

                oops, posted to soon.


                and then


                
                <s:selectItems 
                
                     value="#{list}" 
                
                     var="item" 
                
                     label="#{messages[item.key]}"/>
                
                <s:convertEnum />
                
                



                Which is pretty elegant. This was suggested to my on the old forums...

                • 5. Re: SelectItems, Hibernate and i18n

                  Thanks Nicklas, it works really good!