我有一个枚举,我试图将其国际化,如:
public enum ActivityEnum {
PROJECT_CREATED, PROJECT_EDITED, PROJECT_DONATION;
@Override
public String toString() {
switch (this) {
case PROJECT_CREATED:
return Messages.get("activity.project.created");
case PROJECT_EDITED:
return Messages.get("activity.project.edited");
case PROJECT_DONATION:
return Messages.get("activity.project.donation");
default: return super.toString();
}
}
private ActivityEnum activityEnum;
public ActivityEnum getActivityEnum() {
return activityEnum;
}
public void setActivityEnum(ActivityEnum activityEnum) {
this.activityEnum = activityEnum;
}
}
现在,请注意我的messages.properties中定义的键包含字符串格式化程序,如:
activity.project.created = User <%s> created project <%s>
嗯,问题是,当我在代码中的其他地方对此枚举执行某些操作时,会引发异常,例如:
MissingFormatArgumentException occured : Format specifier 's'
所以基本上它想要当我在toString
enum类中获取密钥时,也要使用args进行格式化,但这只是我以后才使用枚举...
activity.summary = String.format(activityEnum.toString(), args);
你能否告诉我如何处理这个问题?
更新:
如果我使用经典方式,一切都很好用:
PROJECT_CREATED("User <%s> created project <%s>"), PROJECT_EDITED(
"User <%s> edited project <%s>"), PROJECT_DONATION(
"User <%s> made a donation of <%d> to project <%s>");
但这没有国际化。
答案 0 :(得分:1)
编写如下方法:
public String toDisplayString(String ... args){ ... }
摆脱开关声明