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