title: Internationalization of multiple enums (translation of enum values)
body: <p>Again something that has been discussed <a href="https://stackoverflow.com/questions/4375578/jsf-best-way-to-enum-internationalization-i18n">before</a> and where I wanted to share "my" solution and ask for enhancements, other approaches or best practices.</p>
  
  <p>I have several enums where I need internationalization (I need the enum values translated into some languages in order to display them in a jsf page). Examle enum:</p>
  
  <pre><code>public enum TransferStatus {
    NOT_TRANSFERRED,
    TRANSFERRED
  }
  </code></pre>
  
  <p>Translation would be for example <code>Not yet transferred</code> / <code>Transferred, all good</code></p>
  
  <p>The translation should be stored in a MessageBundle (properties files). I was searching for an easy, generic solution (best would be without the need of writing extra code in all the enums) that does not need much on the jsf side. Just to mention it, of course it it possible that two different enums shae the same enum value (e.g. values like <code>COMPLETED</code> that have a different meaning in different enums).</p>
  
  <p>The solution I came up with:</p>
  
  <p>(1) Store translations in the properties file like this:</p>
  
  <pre><code>TransferStatus.NOT_TRANSFERRED = Not yet transferred
  TransferStatus.TRANSFERRED = Transferred, all good
  </code></pre>
  
  <p>(2) Make a helper class that takes an enum and generates the lookup key:</p>
  
  <pre><code>public class EnumTranslator {
    public static String getMessageKey(Enum&lt;?&gt; e) {
      return e.getClass().getSimpleName() + '.' + e.name();
    }
  }
  </code></pre>
  
  <p>(3) Add this code to every enum:</p>
  
  <pre><code>public String getKey() {
    return EnumTranslator.getMessageKey(this);
  }
  </code></pre>
  
  <p>(4) Now, I can access the translated values of my enums like this:</p>
  
  <pre><code>&lt;h:outputText value="#{enum[order.transferStatus.key]}" /&gt;
  </code></pre>
  
  <p>Which is okay, but what I just don't like is adding the same <code>getKey()</code> method to every enum. There should be something better that that! Now it's your turn, SO :-)</p>