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