I am attempting to maintain separate files for different types of "messages" (user messages, field labels/buttons, data values). In this attempt I am trying to get the description of different data values. For example, a UserStatus "A" might be displayed as Active or Activo.
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF-8"/>
<property name="basenames">
<list>
<value>WEB-INF/locale/messages/messages</value>
<value>WEB-INF/locale/fields/fields</value>
<value>WEB-INF/locale/values/values</value>
</list>
</property>
</bean>
![Location of values files.][1]
Here are the relevant entries in the values_en_us.properties file:
user.status.A = Active
user.status.I = Inactive
user.status.P = Pending
I would like to build an Enum for each type of value that I can get the localized value from. In the code below, I have hardcoded the values being passed to the get message to reduce the number of variables when trying to debug this.
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public enum UserStatusEnum {
A,I,P;
private static HashMap<LocaleEnum, EnumMap<UserStatusEnum, String>> userStatuses = new HashMap<LocaleEnum, EnumMap<UserStatusEnum, String>>();
public static EnumMap<UserStatusEnum, String> getUserStatuses(LocaleEnum locale) {
return userStatuses.get(locale);
}
// Pre-load the descriptive values.
static {
String description = null;
ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring-config.xml");
for (LocaleEnum locale : LocaleEnum.values()) {
EnumMap<UserStatusEnum, String> localeSpecificUserStatuses = new EnumMap<UserStatusEnum, String>(UserStatusEnum.class);
for (UserStatusEnum status : UserStatusEnum.values()) {
//description = appCtx.getMessage("user.status."+status, null, new Locale(locale.toString()));
description = appCtx.getMessage("user.status.A", null, new Locale("en_us"));
if (description != null) {
localeSpecificUserStatuses.put(status, description);
}
description=null;
}
userStatuses.put(locale, localeSpecificUserStatuses);
}
}
}
When this code runs, I get the following message:
org.springframework.context.NoSuchMessageException: No message found under code 'user.status.A' for locale 'en_us'.
On this line of code:
description = appCtx.getMessage("user.status.A", null, new Locale("en_us"));
The application is already showing my custom application error messages I have in the messages localized files, but in that case the ApplicationContext is already available. Because I can get to the contents of the messages_en_us.property files, I'm assuming me config is correct. However, the classes that get the messages content are instantiated by Spring.
The Enums are not created by Spring, so my assumption is that I am doing something wrong in how I am getting a handle on the ApplicationContext or how I am using it.
All help is very much appreciated.
Thank You
EDIT: After looking at the appCtx values in debug mode, I can see that the messageSource > basenames does at least contain my configuration data.
[WEB-INF/locale/messages/messages, WEB-INF/locale/fields/fields, WEB-INF/locale/values/values]
Although I don't see the whole filenames with the file extension anywhere.