2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
30 import jalview.bin.Console;
34 * @author David Roldan Martinez
35 * @author Thomas Abeel
39 public class MessageManager
41 private static ResourceBundle rb;
43 private static Locale loc;
45 private static Set<String> reportedMissing = new HashSet<>();
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))
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
65 Console.warn("Exception when initting Locale for i18n messages\n"
70 Console.warn("Error when initting Locale for i18n messages\n"
78 * Returns the resource bundle text for the given key, or if not found, the
79 * key prefixed by "[missing key]"
84 public static String getString(String key)
86 String value = "[missing key] " + key;
89 value = rb.getString(key);
92 String msg = "I18N missing: " + loc + "\t" + key;
98 public static Locale getLocale()
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]"
112 public static String formatMessage(String key, Object... params)
116 return MessageFormat.format(rb.getString(key), params);
117 } catch (Exception e)
119 Console.warn("I18N missing: " + loc + "\t" + key);
121 String value = "[missing key] " + key + "";
122 for (Object p : params)
124 value += " '" + p.toString() + "'";
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]"
138 public static String formatMessage(String key, String[] params)
140 return formatMessage(key, (Object[]) params);
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.
149 * If the key doesn't resolve, then {@code name} is returned.
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).
159 public static String getStringOrReturn(String keyroot, String name)
161 String smkey = keyroot
162 + name.toLowerCase(Locale.ROOT).replaceAll(" ", "");
165 name = rb.getString(smkey);
166 } catch (Exception x)
168 String msg = "I18N missing key with root " + keyroot + ": " + loc
170 logWarning(smkey, msg);
176 * Logs missing keys (each key once only per run)
181 private static void logWarning(String key, String msg)
183 if (!reportedMissing.contains(key))
185 reportedMissing.add(key);