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 org.apache.log4j.Logger;
34 * @author David Roldan Martinez
35 * @author Thomas Abeel
39 public class MessageManager
42 // BH 2018 switched to org.apache.llog4j.Logger
44 private static ResourceBundle rb;
46 private static Logger log = Logger
47 .getLogger(MessageManager.class.getCanonicalName());
49 private static Locale loc;
51 private static Set<String> reportedMissing = new HashSet<>();
57 /* Localize Java dialogs */
58 loc = Locale.getDefault();
59 // Locale.setDefault(loc);
60 /* Getting messages for GV */
61 log.info("Getting messages for lang: " + loc);
62 Control control = Control.getControl(Control.FORMAT_PROPERTIES);
63 rb = ResourceBundle.getBundle("lang.Messages", loc, control);
64 // if (log.isLoggable(Level.FINEST))
66 // // this might take a while, so we only do it if it will be shown
67 // log.info("Language keys: " + rb.keySet()); // was FINEST
71 log.warn("Exception when initting Locale for i18n messages\n"
76 log.warn("Error when initting Locale for i18n messages\n"
83 public static String getString(String key)
85 String value = "[missing key] " + key;
88 value = rb.getString(key);
91 String msg = "I18N missing: " + loc + "\t" + key;
97 public static Locale getLocale()
102 public static String formatMessage(String key, Object... params)
104 return MessageFormat.format(getString(key), params);
107 public static String formatMessage(String key, String[] params)
109 return MessageFormat.format(getString(key), (Object[]) params);
113 * Looks up and returns a key given a root and a human-readable(ish) name that
114 * when combined might resolve to an i18n string. If the key doesn't resolve,
115 * then name is returned. Use this for programmatically constructed keys that
116 * have a human readable alternative used in the program (e.g. BLOSUM62 and
117 * label.score_blosum62).
123 public static String getStringOrReturn(String keyroot, String name)
125 String smkey = keyroot + name.toLowerCase().replaceAll(" ", "");
128 name = rb.getString(smkey);
129 } catch (Exception x)
131 String msg = "I18N missing key with root " + keyroot + ": " + loc + "\t"
133 logWarning(smkey, msg);
139 * Logs missing keys (each key once only per run)
144 private static void logWarning(String key, String msg)
146 if (!reportedMissing.contains(key))
148 reportedMissing.add(key);