JAL-1354 JAL-3528 fix bug from b594d516dac69eebcccdc743933880b00fb6d44c only try...
[jalview.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 /**
30  * 
31  * @author David Roldan Martinez
32  * @author Thomas Abeel
33  * 
34  * 
35  */
36 public class MessageManager
37 {
38
39   private static ResourceBundle rb;
40
41   private static Logger log = Logger
42           .getLogger(MessageManager.class.getCanonicalName());
43
44   private static Locale loc;
45
46   static
47   {
48     try
49     {
50       /* Localize Java dialogs */
51       loc = Locale.getDefault();
52       // Locale.setDefault(loc);
53       /* Getting messages for GV */
54       log.info("Getting messages for lang: " + loc);
55       rb = ResourceBundle.getBundle("lang.Messages", loc);
56       if (log.isLoggable(Level.FINEST))
57       {
58         // this might take a while, so we only do it if it will be shown
59         log.finest("Language keys: " + rb.keySet());
60       }
61     } catch (Exception q)
62     {
63       log.warning("Exception when initting Locale for i18n messages\n"
64               + q.getMessage());
65       q.printStackTrace();
66     } catch (Error v)
67     {
68       log.warning("Error when initting Locale for i18n messages\n"
69               + v.getMessage());
70       v.printStackTrace();
71     }
72
73   }
74
75   public static String getString(String key)
76   {
77     String value = "[missing key] " + key;
78     try
79     {
80       value = rb.getString(key);
81     } catch (Exception e)
82     {
83       log.warning("I18N missing: " + loc + "\t" + key);
84     }
85     return value;
86   }
87
88   public static Locale getLocale()
89   {
90     return loc;
91   }
92
93   public static String formatMessage(String key, Object... params)
94   {
95     try
96     {
97       return MessageFormat.format(rb.getString(key), params);
98     } catch (Exception e)
99     {
100       log.warning("I18N missing: " + loc + "\t" + key);
101       // e.printStackTrace();
102
103     }
104     String value = "[missing key] " + key + "";
105     for (Object p : params)
106     {
107       value += " '" + p.toString() + "'";
108     }
109     return value;
110   }
111
112   public static String formatMessage(String key, String[] params)
113   {
114     return formatMessage(key, (Object[]) params);
115   }
116
117   /**
118    * lookup and return a key given a root and a human-readable(ish) name that
119    * when combined might resolve to an i18n string. If the key doesn't resolve,
120    * then name is returned.if the key doesn't exist. Use this for
121    * programatically constructed keys that have have a human readable
122    * alternative used in the program (e.g. BLOSUM62 and label.score_blosum62)
123    * 
124    * @param keyroot
125    * @param name
126    * @return
127    */
128   public static String getStringOrReturn(String keyroot, String name)
129   {
130     String smkey = keyroot + name.toLowerCase().replaceAll(" ", "");
131     try
132     {
133       name = rb.getString(smkey);
134     } catch (Exception x)
135     {
136       log.finest("I18N missing key with root " + keyroot + ": " + loc + "\t"
137               + smkey);
138     }
139     return name;
140   }
141 }