JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / jalview / util / MessageManager.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.util;
22
23 import java.text.MessageFormat;
24 import java.util.Locale;
25 import java.util.ResourceBundle;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
29 import javajs.J2SRequireImport;
30
31 /**
32  * 
33  * @author David Roldan Martinez
34  * @author Thomas Abeel
35  * 
36  * 
37  */
38 @J2SRequireImport ({java.text.MessageFormat.class})
39 public class MessageManager
40 {
41
42   private static ResourceBundle rb;
43
44   private static Logger log = Logger.getLogger(MessageManager.class
45           .getCanonicalName());
46
47   private static Locale loc;
48
49   static
50   {
51     try
52     {
53       /* Localize Java dialogs */
54       loc = Locale.getDefault();
55       // Locale.setDefault(loc);
56       /* Getting messages for GV */
57       log.info("Getting messages for lang: " + loc);
58       rb = ResourceBundle.getBundle("lang.Messages", loc);
59       // BH rb == null just throws the exception 
60       if (rb == null || log.isLoggable(Level.FINEST))
61       {
62         // this might take a while, so we only do it if it will be shown
63         log.finest("Language keys: " + rb.keySet());
64       }
65     } catch (Exception q)
66     {
67       log.warning("Exception when initting Locale for i18n messages\n"
68               + q.getMessage());
69       q.printStackTrace();
70     } catch (Error v)
71     {
72       log.warning("Error when initting Locale for i18n messages\n"
73               + v.getMessage());
74       v.printStackTrace();
75     }
76
77   }
78
79   public static String getString(String key)
80   {
81     String value = "[missing key] " + key;
82     try
83     {
84       value = rb.getString(key);
85     } catch (Exception e)
86     {
87       log.warning("I18N missing: " + loc + "\t" + key);
88     }
89     return value;
90   }
91
92   public static Locale getLocale()
93   {
94     return loc;
95   }
96
97   public static String formatMessage(String key, Object... params)
98   {
99     return MessageFormat.format(getString(key), params); // BH not rb.getString
100   }
101
102   public static String formatMessage(String key, String[] params)
103   {
104     return MessageFormat.format(getString(key), (Object[]) params); // BH not rb.getString
105   }
106
107   /**
108    * lookup and return a key given a root and a human-readable(ish) name that
109    * when combined might resolve to an i18n string. If the key doesn't resolve,
110    * then name is returned.if the key doesn't exist. Use this for
111    * programatically constructed keys that have have a human readable
112    * alternative used in the program (e.g. BLOSUM62 and label.score_blosum62)
113    * 
114    * @param keyroot
115    * @param name
116    * @return
117    */
118   public static String getStringOrReturn(String keyroot, String name)
119   {
120     String smkey = keyroot + name.toLowerCase().replaceAll(" ", "");
121     try
122     {
123       name = rb.getString(smkey);
124     } catch (Exception x)
125     {
126       log.finest("I18N missing key with root " + keyroot + ": " + loc
127               + "\t" + smkey);
128     }
129     return name;
130   }
131 }