I have an enumeration where I need to display the values as localized strings. My current approach has been this:
public enum MyEnum {
VALUE1(R.string.VALUE1),
VALUE2(R.string.VALUE2),
.
.
VALUE10(R.string.VALUE10);
private int mResId = -1;
private MuEnum(int resId) {
mResId = resId;
}
public String toLocalizedString(Resources r) {
if (-1 != mResId) return (r.getString(mResId));
return (this.toString());
}
}
Is there any easier way to to do this? I'd love it if I could somehow lookup the resource based on the enumeration value name (i.e 'VALUE1').
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="VALUE1"/>My string</string>
<string name="VALUE2"/>My string 2</string>
.
.
<string name="VALUE10"/>My string 3</string>
</resources>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EDIT: Just for future reference, this is the solution that worked best for me:
public enum MyEnum {
VALUE1,
VALUE2,
.
.
VALUE10;
/**
* Returns a localized label used to represent this enumeration value. If no label
* has been defined, then this defaults to the result of {@link Enum#name()}.
*
* <p>The name of the string resource for the label must match the name of the enumeration
* value. For example, for enum value 'ENUM1' the resource would be defined as 'R.string.ENUM1'.
*
* @param context the context that the string resource of the label is in.
* @return a localized label for the enum value or the result of name()
*/
public String getLabel(Context context) {
Resources res = context.getResources();
int resId = res.getIdentifier(this.name(), "string", context.getPackageName());
if (0 != resId) {
return (res.getString(resId));
}
return (name());
}
}
An enum pattern, in which each enumerated case is associated to an instance of a generic attribute class, is identified and implemented. It is particularly useful in situations where there is a finite number of cases and each case is characterized by a set of constants.
The Enum in Java is a data type which contains a fixed set of constants.
A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.
You can certainly look up a resource by its name using Resources.getIdentifier()
. For instance, with the string resources you posted as an example, you can do this from an activity:
Resources res = getResources();
MyEnum e = MyEnum.VALUE1;
String localized = res.getString(res.getIdentifier(e.name(), "string", getPackageName()));
From a View, you'd have to change the last argument to getContext().getPackageName()
I think what you have tried is good except you dont need to pass the resources argument to the enum every time you want to translate the enum.
Use the link to subclass the Application Class, then follow this approach.
import android.app.Application;
public enum MyEnum {
VALUE1(R.string.VALUE1),
VALUE2(R.string.VALUE2),
.
.
VALUE10(R.string.VALUE10);
private int resourceId;
private MyEnum(int id) {
resourceId = id;
}
@Override
public String toString() {
return MyApplication.getApplicationContext().getString(resourceId);
}
}
Then calling MyEnum.VALUEx
will always give you the translated enum value, but be careful this might not be what you want always e.g you may have a raw query like this:
select * from Customer where userStatus = MyEnum.VALUEx.toString();
This may break your app, if you are storing the enum values as VALUE1, VALUE2... in db, so remember to use this MyEnum.VALUEx.name()
when you dont want to use the translated value of your MyEnum
.
select * from Customer where userStatus = MyEnum.VALUEx.name();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With