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