I have an enum and I tried to internationalize it like:
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;
}
}
Now, please note that the keys defined in my messages.properties contains string formatters like:
activity.project.created = User <%s> created project <%s>
Well, the issue is that when I do something with this enum in other places in the code, it throws an exception like:
MissingFormatArgumentException occured : Format specifier 's'
so basically it wants that when I get the key in toString
enum class, to apply also the args for formatting but this I only do later where I use that enum...
activity.summary = String.format(activityEnum.toString(), args);
Can you please give me a suggestion of how to handle this?
UPDATE:
If I use the classic way, all works nice:
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>");
but this is without internationalization.
write a method like:
public String toDisplayString(String... args){ ... }
get rid of the switch statement