View Javadoc

1   package pl.caltha.internal.xml;
2   
3   import java.util.HashMap;
4   import java.util.MissingResourceException;
5   import java.util.ResourceBundle;
6   
7   /*** Localizer class for XMLService.
8    *
9    * @author <a href="mailto:zwierzem@ngo.pl">Damian Gajda</a>
10   * @version $Id: Localizer.java,v 1.3 2006/04/28 10:02:24 pablo Exp $
11   */
12  public class Localizer
13  {
14      private static HashMap bundles;
15      
16      public static void init()
17      {
18          addBundle("pl.caltha.internal.xml.Messages");
19          addBundle("com.sun.msv.verifier.Messages");
20      }
21      
22      /*** Adds a new bundle to this localizer. */
23      public static synchronized void addBundle(String baseName)
24      {
25          if(bundles == null)
26          {
27              bundles = new HashMap();
28          }
29  
30          if(bundles.containsKey(baseName))
31          {
32              return;
33          }
34          
35          try
36          {
37              ResourceBundle newBundle = ResourceBundle.getBundle(baseName);
38              bundles.put(baseName, newBundle);
39          }
40          catch(MissingResourceException e)
41          {
42              // bum.....
43          }
44      }
45  
46      /*** Localizes a message with a given property name and arguments. */
47      public static String localize(String propertyName, Object[] args)
48      {
49          if(bundles == null)
50          {
51              init();
52          }
53          
54          for(java.util.Iterator iter = bundles.values().iterator(); iter.hasNext();)
55          {
56              ResourceBundle bundle = (ResourceBundle)(iter.next());
57              try
58              {
59                  String format = bundle.getString(propertyName);
60                  return java.text.MessageFormat.format(format, args);
61              }
62              catch(MissingResourceException e)
63              {
64                  // ignore
65              }
66          }
67          throw new MissingResourceException("Cannot find a localization message",
68                          propertyName.getClass().getName(), propertyName);
69      }
70  
71      /*** Localizes a message with a given property name. */
72      public static String localize(String prop)
73      {
74          return localize(prop, null);
75      }
76  
77      /*** Localizes a message with a given property name and argument. */
78      public static String localize(String prop, Object arg1)
79      {
80          return localize(prop,new Object[]{arg1});
81      }
82  
83      /*** Localizes a message with a given property name and arguments. */
84      public static String localize(String prop, Object arg1, Object arg2)
85      {
86          return localize(prop,new Object[]{arg1,arg2});
87      }
88  }