java – Make Enum. Tostring() localization

I'm developing an Android application and I want to know if I can set enum Tostring() multilingual

I'll use this enumeration on spinner. I want to use multilingual text

public class Types
{
    public enum Stature
    {
        tall (0,"tall"),average(1,"average"),small(2,"small");

        private final int stature;
        private final String statureString;

        Stature(int anStature,String anStatureString) { stature = anStature; statureString = anStatureString; }

        public int getValue() { return stature; }

        @Override
        public String toString() { return statureString; }
    }
}

I don't know how to use context in enum GetString (), and I have hard coded it as "high", "average" and "small" to test it I have defined this enumeration in the help class

So how do I use the enumeration on the spinner box:

mSpinStature.setAdapter(new ArrayAdapter<Stature>(mActivity,android.R.layout.simple_dropdown_item_1line,Stature.values()));

You know what I should do?

resolvent

Solution

Suppose this resource path

String resourceBundlePath = "my.package.bundles.messages"

In the package my package. The bundles package may have messages properties,messages_ en_ US. Properties, etc

Then, use

ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceBundlePath);
String messageKey = "myFirstMessage";
String message = resourceBundle.getMessage(messageKey);

Message will contain message The value of the messagekey property defined on properties If the current locale is actually en_ Us, you will start from messages_ en_ US. Get the value of properties If the current locale is a value that you do not have a properties file, the value will come from the default messages properties

You can also call

ResourceBundle.getBundle(resourceBundlePath,myLocale);

However, it is usually best to use the platform locale (see the JVM parameters - duser. Language, - duser. Country)

You can use resourcebundle for each enumeration that you want to translate using the enumeration element name and use it in the toString () implementation of the enumeration:

@Override
public String toString() {
return resourceBudle.getString(super.toString());
}

The above is the Java – make enum collected by programming house for you Tostring() localizes all the contents. I hope this article can help you solve Java – make enum Program development problems encountered in tostring() localization.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>