View Javadoc

1   /*
2    * $Id: LocaleMessageHandler.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.i18n;
12  
13  import java.text.MessageFormat;
14  import java.util.Locale;
15  import java.util.ResourceBundle;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  
20  /**
21   * The <code>LocaleMessageHandler</code> is essentially a merging of the Message
22   * and Messages classes, since there is no good reason to have them separate. A
23   * key point is that this Handler is meant to be used for application-specific
24   * messages, rather than core system messages. (That's not to say it couldn't
25   * eventually replace the latter, however). Note that message codes are Strings
26   * here, instead of the ints in Message.
27   * <p/>
28   * The LocaleMessageHandler can be called directly, but is really meant to be
29   * called by LocaleMessage classes as done in the examples.
30   * <p/>
31   * Note that this class assumes the resource bundle is in the format
32   * <bundle-name>-messages and is located at the top of the jar or classes
33   * directory. We can later add the ability to specify a path prefix.
34   */
35  public class LocaleMessageHandler
36  {
37      /**
38       * logger used by this class
39       */
40      protected static final Log logger = LogFactory.getLog(LocaleMessageHandler.class);
41  
42      /**
43       * Get the resource string for the given bundle name and resource code
44       */
45      public static String getString(String bundleName, String code)
46      {
47          return getString(bundleName, code, new Object[]{});
48      }
49  
50      /**
51       * Get the resource string for the given bundle name, resource code and
52       * one argument
53       */
54      public static String getString(String bundleName, String code, Object arg1)
55      {
56          if (arg1 == null)
57          {
58              arg1 = "null";
59          }
60  
61          return getString(bundleName, code, new Object[]{arg1});
62      }
63  
64      /**
65       * Get the resource string for the given bundle name, resource code and
66       * two arguments
67       */
68      public static String getString(String bundleName, String code, Object arg1, Object arg2)
69      {
70          if (arg1 == null)
71          {
72              arg1 = "null";
73          }
74  
75          if (arg2 == null)
76          {
77              arg2 = "null";
78          }
79  
80          return getString(bundleName, code, new Object[]{arg1, arg2});
81      }
82  
83      /**
84       * Get the resource string for the given bundle name, resource code and array
85       * of arguments.
86       */
87      public static String getString(String bundleName, String code, Object[] args)
88      {
89          return getString(bundleName, Locale.getDefault(), code, args);
90      }
91  
92      /**
93       * Get the resource string for the given bundle name, locale, resource code and array
94       * of arguments. All above methods invoke this one.
95       */
96      public static String getString(String bundleName, Locale locale, String code, Object[] args)
97      {
98          String path = bundleName + "-messages";
99          ResourceBundle bundle = ResourceBundle.getBundle(path, locale);
100         String m = bundle.getString(code);
101 
102         if (m == null)
103         {
104             logger.error("Failed to find message for id " + code + " in resource bundle " + path);
105             return "";
106         }
107 
108         return MessageFormat.format(m, args);
109     }
110 
111 }
112