Joshua Java

Localizing enums with Seam

Posted on: March 26, 2008

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

public class Person {

	public enum Gender {

 	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 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}" />


3 Responses to "Localizing enums with Seam"

Bos…, yang gambar kedua itu maksudnya apa? Itu pake bahasa apa sih?

This is going to work but it’s not very elegant. You mix intenralization logic with your code. Better approach would be defining Gender like this (hope this form would accept tabs in code):

public enum Gender {  
MALE ( "key.for.male") ,  
FEMALE ( "key.for.female" );  

	Gender( String key ) {
		this.key = key;
	public String getKey() {
		return key;

and displaying it in jsf like this:

<h:outputText id=”gender” value=”#{messages&#91;client.gender.key&#93;}”/>

Hi Tomasz,

Your solution is only elegant when you already the value of gender, but mine is targeted for localizing gender which value is retrieved from database. Thanks for the sharing!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Flickr Photos

%d bloggers like this: