You can replace the DefaultPropertiesPersister with this one:
This will allow you to reference other entries, e.g.:
user=User
user.add=Add ${user}
user.delete=Delete ${user}
Simply specify this persister with your MessageSource, e.g. messageSource.setPropertiesPersister(new RecursivePropertiesPersister());
Source:
public class RecursivePropertiesPersister extends DefaultPropertiesPersister {
private final static Pattern PROP_PATTERN = Pattern.compile("\\$'?\\{'?([^}']*)'?\\}'?");
private final static String CURRENT = "?LOOP?";
@Override
public void load(Properties props, Reader reader) throws IOException {
Properties propsToLoad = new Properties();
super.load(propsToLoad, reader);
replace(propsToLoad, props);
}
@Override
public void load(Properties props, InputStream is) throws IOException {
Properties propsToLoad = new Properties();
super.load(propsToLoad, is);
replace(propsToLoad,props);
}
protected void replace ( Properties src, Properties dest) {
for (Map.Entry entry: src.entrySet()) {
String key = (String) entry.getKey();
String value = (String)entry.getValue();
replace(src, dest, key, value);
}
}
protected String replace(Properties src, Properties dest, String key, String value) {
String replaced = (String) dest.get(key);
if (replaced != null) {
// already replaced (or loop), just return the string
return replaced;
}
dest.put(key,CURRENT); // prevent loops
final Matcher matcher = PROP_PATTERN.matcher(value);
final StringBuffer sb = new StringBuffer();
while (matcher.find()) {
final String subkey = matcher.group(1);
final String replacement = (String)src.get(subkey);
matcher.appendReplacement(sb,replace(src,dest,subkey,replacement));
}
matcher.appendTail(sb);
final String resolved = sb.toString();
dest.put(key, resolved);
return resolved;
}
}