VSB VSB - 1 year ago 57
Android Question

Android: enums with values which can be translated to other locales

Suppose I have below


public enum Feelings {
private final String name;
private Feelings(String s) {
name = s;
public boolean equalsName(String otherName) {
return (otherName == null) ? false : name.equals(otherName);
public String toString() {
return name;

when I call its
it returns labels defined for different enumeration. I use these labels on UI and they will be displayed to users.

When I consider to publish my app with different locales, It comes to my mindhow can I define these labels such that they can be translated to other languages?

Answer Source

Instead of setting the actual value of the label in the enum you could use the string resource id.

public enum Feelings {
  happy(R.string.happy_label), sad(R.string.sad_label);
  private final int strId;

  private Feelings(int strId) {
    this.strId = strId;

  @StringRes public int getStringId() {
    return strId;

This way Android will pick the correct translation of your strings given the device's locale

Your usage could then look like this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download