01: package org.andromda.cartridges.jsf;
02:
03: import java.text.MessageFormat;
04:
05: import java.util.MissingResourceException;
06: import java.util.ResourceBundle;
07:
08: import javax.faces.context.FacesContext;
09:
10: /**
11: * Provides access to the application resource messages.
12: */
13: public class Messages {
14: /**
15: * The messages bundle name.
16: */
17: private static final String BUNDLE_NAME = "message-resources";
18:
19: /**
20: * Gets the message given the <code>key</code> by substituting any
21: * required <code>arguments</code>.
22: *
23: * @param key the message key.
24: * @param arguments any message arguments to substitute.
25: * @return the message (or key if the message isn't found).
26: */
27: public static String get(String key, Object[] arguments) {
28: FacesContext context = FacesContext.getCurrentInstance();
29: String resourceString;
30: try {
31: final ResourceBundle bundle = ResourceBundle.getBundle(
32: BUNDLE_NAME, context.getViewRoot().getLocale());
33: resourceString = bundle.getString(key);
34: if (arguments != null) {
35: final MessageFormat format = new MessageFormat(
36: resourceString, context.getViewRoot()
37: .getLocale());
38: resourceString = format.format(arguments);
39: }
40: } catch (final MissingResourceException exception) {
41: resourceString = key;
42: }
43: return resourceString;
44: }
45: }
|