JAL-1521 updated xerces to 2.11 along with xml-apis
[jalview.git] / src / jalview / util / MessageManager.java
1 package jalview.util;
2
3 import java.text.MessageFormat;
4 import java.util.Locale;
5 import java.util.ResourceBundle;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8
9 /**
10  * 
11  * @author David Roldan Martinez
12  * @author Thomas Abeel
13  * 
14  *
15  */
16 public class MessageManager {
17
18         private static ResourceBundle rb;    
19                 
20         private static Logger log=Logger.getLogger(MessageManager.class.getCanonicalName());
21         
22         private static Locale loc;
23         
24         
25         
26     static{
27       try {
28         /* Localize Java dialogs */
29         loc = Locale.getDefault();
30         // Locale.setDefault(loc);
31         /* Getting messages for GV */
32         log.info("Getting messages for lang: "+loc);
33         rb = ResourceBundle.getBundle("lang.Messages", loc);
34         if (log.isLoggable(Level.FINEST)) {
35           // this might take a while, so we only do it if it will be shown
36           log.finest("Language keys: "+rb.keySet());
37         }
38       } catch (Exception q) {
39         log.warning("Exception when initting Locale for i18n messages\n"+q.getMessage());
40         q.printStackTrace();
41       }
42       catch (Error v)
43       {
44         log.warning("Error when initting Locale for i18n messages\n"+v.getMessage());
45         v.printStackTrace();
46       }
47       
48      
49     }
50     
51     public static String getString(String key){
52         String value = "[missing key] " + key;
53         try{
54                 value = rb.getString(key);
55         }catch(Exception e){
56           log.warning("I18N missing: "+loc+"\t"+key);
57         }
58         return value;
59     }
60     
61         public static Locale getLocale() {
62                 return loc;
63         }
64         public static String formatMessage(String key, Object... params){
65                 return MessageFormat.format(rb.getString(key), (Object[]) params);
66         }
67
68   /**
69    * lookup and return a key given a root and a human-readable(ish) name that when combined might resolve to an i18n string.
70    * If the key doesn't resolve, then name is returned.if the key doesn't exist.
71    * Use this for programatically constructed keys that have have a human readable alternative used in the program (e.g. BLOSUM62 and label.score_blosum62) 
72    * @param keyroot
73    * @param name
74    * @return
75    */
76   public static String getStringOrReturn(String keyroot, String name)
77   {
78     String smkey = keyroot
79             + name.toLowerCase().replaceAll(" ", "");
80     try {
81       name = rb.getString(smkey); 
82     }
83     catch (Exception x) {
84       log.finest("I18N missing key with root "+keyroot+": "+loc+"\t"+smkey);
85     }
86     return name;
87   }
88 }