Suppose I have below enum:
public enum Feelings {
happy("label1"),sad("label2")
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 .toString() 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?
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:
textView.setText(Feelings.happy.getStringId());