枚举
public enum EmployeeStatus {
ACTIVE, IN_ACTIVE
}
在遍布应用程序的调用者中,获取并设置如下所示的枚举。以下是示例
Caller_1
if(employee.getStatus() == EmployeeStatus.STATUS.SUBMITTED) {
}
Caller_2
employee.setStatus(EmployeeStatus.STATUS.SUBMITTED)
问题
我需要实现国际化,以便最终用户根据区域设置查看员工状态。所以当我设置法语区域设置的状态时 它应该从正确的资源bundel设置值。有没有办法在不改变调用者代码的情况下实现这一点。这是解决方案 我能想到
我建议的解决方案: -
public enum EmployeeStatus {
ACTIVE, IN_ACTIVE
public static String toString() {
return I18n.getMessage("label." + this);
}
}
public final class I18n {
private I18n() {
}
private static ResourceBundle bundle;
public static String getMessage(String key) {
if(bundle == null) {
bundle = ResourceBundle.getBundle("path.to.i18n.messages");
}
return bundle.getString(LocaleContextHolder.getLocale());
}
}
通过这种方法,我需要在每个枚举中添加toString
方法,而无需更改调用者?春天提供了更好的通用方法吗?
我正在使用弹簧4.看看春天是否有帮助。