This discussion is archived
9 Replies Latest reply: Mar 9, 2013 7:07 PM by 893253 RSS

enumeration and internationalization

REDO LOG Newbie
Currently Being Moderated
Hi all,
How can we combine enumeration with internationalization in java?

here is an example that I worked on it but get confused

we have an enumeration Title as :
package internationalisation;


public enum Title {
     Mr, Ms, Miss;
}
and the Employee entity:
package entities;


@Entity
public class Employee{
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;

  @Column(name="name", length = 30)
  private String name;
  
  @Enumerated(EnumType.ORDINAL)
  @Column(name="title", columnDefinition = "char(1)")
  private Title title;
  
   // getters ans setters
   

}
in the JSF form the title flied will display the title values in a selectOneMenu tag depending on the langauge selected :

for example: for English : Mr, Ms, Miss and for French: Mr, Madame, Mlle form,

to do so I needed properties files Title-US and Title-FR witch contains the following:
# Title-FR file

Mr = Mr
Miss = Mlle
Ms = Madame
#Title-US file
Mr = Mr
Miss= Miss
Ms = Ms
how can I make the enumeration do this??

I tried this but get confused :
package localisation;

import java.util.ResourceBundle;

public enum Title {
     Mr, Ms, Miss;

     private static String language;

     public String getLanguage() {
          return language;
     }

     public void setLanguage(String language) {
          Title.language = language;
     }

     
     private static final ResourceBundle res = ResourceBundle.getBundle("Title-"+language+".properties");

     public String toString() {
          return res.getString(name());
     }

}
thanks for help
  • 1. Re: enumeration and internationalization
    893253 Explorer
    Currently Being Moderated
    http://docs.oracle.com/javase/tutorial/i18n/intro/steps.html

    As per the link above, you can use ResourceBundle as below;
    messages = ResourceBundle.getBundle("MessagesBundle", currentLocale); // in your case replace MessagesBundle with Title.

    It will automatically select right property file to refer according to passed locale.
  • 2. Re: enumeration and internationalization
    REDO LOG Newbie
    Currently Being Moderated
    hi thanks for replying,

    but how can I put this with enumerations?
  • 3. Re: enumeration and internationalization
    893253 Explorer
    Currently Being Moderated
    public enum Title {
         Mr, Ms, Miss;
    }
    The enum you defined is the key to retrieved the Locale specific values . So the usage is pretty straight forward:
    messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
    String msg1 = messages.getString("greetings"); // *Now you should replace "greetings" with your enum elements*
    See the comments in above code and do accordingly, I think you can proceed now to complete your code. Isn't it?
  • 4. Re: enumeration and internationalization
    REDO LOG Newbie
    Currently Being Moderated
    yes that worked for me

    but here is the situation:


    I have an enumeration attribute in the entity, annotated as an ORDINAL enumeration
    in the jsf form , I have:
    <p:selectOneMenu value="#{employeeBean.employee.title}">
            <f:selectItems value="#{employeeBean.titles}" var="title"
                            itemLabel="#{title}"
                            itemValue="#{title}"/>
          </p:selectOneMenu>
    to select the title of the employee,
    now as the Title enumeration is ORDINAL, the values that will be stored in the database will be 0, 1, 2 if Mr, Ms, Miss are selected (respectively)
    the problem is how to make that for the two languages, I have to find out how to combine the enumeration the properties files so that if I pick Miss for English, or Mlle for French, the values that the enumeration will have is Miss (so that the values submitted to the database is 2 : 2 for Miss)
  • 5. Re: enumeration and internationalization
    893253 Explorer
    Currently Being Moderated
    now as the Title enumeration is ORDINAL, the values that will be stored in the database will be 0, 1, 2 if Mr, Ms, Miss are selected (respectively)
    the problem is how to make that for the two languages, I have to find out how to combine the enumeration the properties files so that if I pick Miss for English, or Mlle for French, the values that the enumeration will have is >Miss (so that the values submitted to the database is 2 : 2 for Miss)
    I think a if-else condition could be of help in this case
  • 6. Re: enumeration and internationalization
    REDO LOG Newbie
    Currently Being Moderated
    I don't think so,
    the problem is the <f:selectItems > tag, I have to find out hot to change it according to the selected language
  • 7. Re: enumeration and internationalization
    893253 Explorer
    Currently Being Moderated
    I think you need enum just to display the Select menu items. And after it is rendered in your form you need to save it to database (may be after clicking some button and hence submitting the form).
    I am not sure about jsf but most APIs for select menu have some property called "SelectedIndex" or so which returns the indexed positions(i.e 0,1,2..the indexes you need) of items in menu.

    So if you could find this property you just need to retrieve and save its value in database.

    Edited by: J2EE_Life on Mar 9, 2013 3:34 PM
  • 8. Re: enumeration and internationalization
    gimbal2 Guru
    Currently Being Moderated
    I dont get this thread at all. Dont use the title as the select item value, use the enum object.
  • 9. Re: enumeration and internationalization
    893253 Explorer
    Currently Being Moderated
    public enum Title {
         Mr, Ms, Miss;
    }
    And then
    private Title title;
    So title is the enum object.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points