使Enum.toString()本地化 [英] Make Enum.toString() localized
问题描述
Enum.toString()
多语言。 我将在 Spinner
上使用这个枚举
,我想使用多语言文本。
public class Types
{
public enum Stature
{
tall( 0,高),
平均(1,平均),
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;
}
}
我不知道如何使用$一个枚举内的c $ c> Context.getString(),我已经硬编码为高,平均和小来测试它。我已经在帮助类中定义了枚举
。
这个我如何使用枚举
Spinner
:
mSpinStature .setAdapter(new ArrayAdapter< Stature>(mActivity,android.R.layout.simple_dropdown_item_1line,Stature.values()));
你知道我该怎么做?
假设这个资源路径
String resourceBundlePath =my.package.bundles.messages
在包 my.package .bundles
您可能有 messages.properties
, messages_en_US.properties
等。
然后,使用
ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceBundlePath);
String messageKey =myFirstMessage;
String message = resourceBundle.getMessage(messageKey);
消息
将包含 messageKey
在 messages.properties
中定义的属性。如果当前的区域设置实际上是 en_US
,您将从 messages_en_US.properties
获取值。如果当前的区域设置是您没有的属性文件,则该值将从默认的 messages.properties
您还可以调用
ResourceBundle.getBundle(resourceBundlePath,myLocale);
但通常最好使用平台语言环境(看看jvm参数-Duser.language ,-Duser.country)
您可以为每个要使用枚举元素名称翻译的枚举创建一个ResourceBundle,并将其用于您的枚举:
@Override
public String toString(){
return resourceBudle.getString(super.toString ());
}
I'm developing an Android application and I want to know if I can set Enum.toString()
multilanguage.
I'm going to use this Enum
on a Spinner
and I want to use multi language texts.
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.getString()
inside an Enum, and I have hardcoded "tall", "average" and "small" to test it. I have defined that enum
inside on a helper class.
This how I use the enum
on a Spinner
:
mSpinStature.setAdapter(new ArrayAdapter<Stature>(mActivity, android.R.layout.simple_dropdown_item_1line, Stature.values()));
Do you know how can I do it?
Assume this resource path
String resourceBundlePath = "my.package.bundles.messages"
In package my.package.bundles
you may have messages.properties
, messages_en_US.properties
etc.
Then, using
ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceBundlePath);
String messageKey = "myFirstMessage";
String message = resourceBundle.getMessage(messageKey);
message
will contain the value of the messageKey
property defined on messages.properties
. If the current Locale is actually en_US
you will get the value from messages_en_US.properties
. If the current locale is something you do not have a properties file for the value will be from the default messages.properties
You can also call
ResourceBundle.getBundle(resourceBundlePath, myLocale);
but it is generally better to use the platform locale (have a look at jvm arguments -Duser.language, -Duser.country)
You can have a ResourceBundle for each enum you want to translate with keys the enum element names and use it in the toString() implementation of your enum:
@Override
public String toString() {
return resourceBudle.getString(super.toString());
}
这篇关于使Enum.toString()本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!