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