Joshua Java

Posts Tagged ‘localization

Wow, I just realized that I haven’t wrote anything in this blog for more than a month. Shame on me. Ok here’s another code that hopefully be beneficial. Recently I had a project where I need to display the enum values in local language. Thanks God for Seam I can easily do this.
In your model class (e.g Person.java)

public class Person {

	public enum Gender {
 	MALE,
 	FEMALE;

		@Override
 	public String toString() {
 		switch (this){
 			case MALE: return ResourceBundle.instance().getString("male");
 			case FEMALE: return ResourceBundle.instance().getString("female");
 			default: return super.toString();
 		}
 	}

	private Gender gender;
 public Gender getGender() {
 	return gender;
 }

	public void setGender(Gender gender) {
 	this.gender = gender;
 }
 };

I had this inner enum inside a Person class and in there I just override toString() method. Inside the method I just call the Seam’s ResourceBundle component and call the appropriate message from my resource bundles which is located in messages.properties.Now when I call the gender property from my view, it will be automatically localized.

        <h:panelGrid columns="2" columnClasses="form-column"
            rowClasses="form-row" styleClass="form-table">

            <h:outputLabel for="gender" value="#{messages&#91;'gender'&#93;}" />
            <h:outputText id="gender" value="#{client.gender}" />

        </h:panelGrid>