JAL-3949 Complete new abstracted logging framework in jalview.log. Updated log calls...
[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.HashSet;
25 import java.util.Locale;
26 import java.util.ResourceBundle;
27 import java.util.ResourceBundle.Control;
28 import java.util.Set;
29
30 import jalview.log.JLoggerLog4j;
31
32 /**
33  * 
34  * @author David Roldan Martinez
35  * @author Thomas Abeel
36  * 
37  * 
38  */
39 public class MessageManager
40 {
41   private static ResourceBundle rb;
42
43   private static JLoggerLog4j log = JLoggerLog4j
44           .getLogger(MessageManager.class.getCanonicalName());
45
46   private static Locale loc;
47
48   private static Set<String> reportedMissing = new HashSet<>();
49
50   static
51   {
52     try
53     {
54       /* Localize Java dialogs */
55       loc = Locale.getDefault();
56       // Locale.setDefault(loc);
57       /* Getting messages for GV */
58       log.info("Getting messages for lang: " + loc);
59       Control control = Control.getControl(Control.FORMAT_PROPERTIES);
60       rb = ResourceBundle.getBundle("lang.Messages", loc, control);
61       // if (log.isLoggable(Level.FINEST))
62       // {
63       // // this might take a while, so we only do it if it will be shown
64       // log.info("Language keys: " + rb.keySet()); // was FINEST
65       // }
66     } catch (Exception q)
67     {
68       log.warn("Exception when initting Locale for i18n messages\n"
69               + q.getMessage());
70       q.printStackTrace();
71     } catch (Error v)
72     {
73       log.warn("Error when initting Locale for i18n messages\n"
74               + v.getMessage());
75       v.printStackTrace();
76     }
77
78   }
79
80   /**
81    * Returns the resource bundle text for the given key, or if not found, the
82    * key prefixed by "[missing key]"
83    * 
84    * @param key
85    * @return
86    */
87   public static String getString(String key)
88   {
89     String value = "[missing key] " + key;
90     try
91     {
92       value = rb.getString(key);
93     } catch (Exception e)
94     {
95       String msg = "I18N missing: " + loc + "\t" + key;
96       logWarning(key, msg);
97     }
98     return value;
99   }
100
101   public static Locale getLocale()
102   {
103     return loc;
104   }
105
106   /**
107    * Returns the resource bundle text for the given key, with tokens {@code {0},
108    * {1} etc replaced by the supplied parameters. If the key is not found,
109    * returns the key and values prefixed by "[missing key]"
110    * 
111    * @param key
112    * 
113    * @return
114    */
115   public static String formatMessage(String key, Object... params)
116   {
117     try
118     {
119       return MessageFormat.format(rb.getString(key), params);
120     } catch (Exception e)
121     {
122       log.warn("I18N missing: " + loc + "\t" + key);
123     }
124     String value = "[missing key] " + key + "";
125     for (Object p : params)
126     {
127       value += " '" + p.toString() + "'";
128     }
129     return value;
130   }
131
132   /**
133    * Returns the resource bundle text for the given key, with tokens {@code {0},
134    * {1} etc replaced by the supplied parameters. If the key is not found,
135    * returns the key and values prefixed by "[missing key]"
136    * 
137    * @param key
138    * 
139    * @return
140    */
141   public static String formatMessage(String key, String[] params)
142   {
143     return formatMessage(key, (Object[]) params);
144   }
145
146   /**
147    * Returns resource bundle text given a root and a human-readable(ish) name
148    * that when combined might resolve to an i18n string. {@code name} is forced
149    * to lower case, with any spaces removed, and concatenated to {@code keyroot}
150    * to form a lookup key.
151    * <p>
152    * If the key doesn't resolve, then {@code name} is returned.
153    * <p>
154    * Use this for programmatically constructed keys that might have a human
155    * readable alternative used in the program (e.g. BLOSUM62 and
156    * label.score_blosum62).
157    * 
158    * @param keyroot
159    * @param name
160    * @return
161    */
162   public static String getStringOrReturn(String keyroot, String name)
163   {
164     String smkey = keyroot
165             + name.toLowerCase(Locale.ROOT).replaceAll(" ", "");
166     try
167     {
168       name = rb.getString(smkey);
169     } catch (Exception x)
170     {
171       String msg = "I18N missing key with root " + keyroot + ": " + loc
172               + "\t" + smkey;
173       logWarning(smkey, msg);
174     }
175     return name;
176   }
177
178   /**
179    * Logs missing keys (each key once only per run)
180    * 
181    * @param key
182    * @param msg
183    */
184   private static void logWarning(String key, String msg)
185   {
186     if (!reportedMissing.contains(key))
187     {
188       reportedMissing.add(key);
189       log.info(msg);
190     }
191   }
192 }