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