JAL-3032 load properties as "java.properties"
[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.ResourceBundle.Control;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 /**
31  * 
32  * @author David Roldan Martinez
33  * @author Thomas Abeel
34  * 
35  * 
36  */
37 public class MessageManager
38 {
39
40   private static ResourceBundle rb;
41
42   private static Logger log = Logger
43           .getLogger(MessageManager.class.getCanonicalName());
44
45   private static Locale loc;
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       log.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.finest("Language keys: " + rb.keySet());
62       }
63     } catch (Exception q)
64     {
65       log.warning("Exception when initting Locale for i18n messages\n"
66               + q.getMessage());
67       q.printStackTrace();
68     } catch (Error v)
69     {
70       log.warning("Error when initting Locale for i18n messages\n"
71               + v.getMessage());
72       v.printStackTrace();
73     }
74
75   }
76
77   public static String getString(String key)
78   {
79     String value = "[missing key] " + key;
80     try
81     {
82       value = rb.getString(key);
83     } catch (Exception e)
84     {
85       log.warning("I18N missing: " + loc + "\t" + key);
86     }
87     return value;
88   }
89
90   public static Locale getLocale()
91   {
92     return loc;
93   }
94
95   public static String formatMessage(String key, Object... params)
96   {
97     return MessageFormat.format(rb.getString(key), params);
98   }
99
100   public static String formatMessage(String key, String[] params)
101   {
102     return MessageFormat.format(rb.getString(key), (Object[]) params);
103   }
104
105   /**
106    * lookup and return a key given a root and a human-readable(ish) name that
107    * when combined might resolve to an i18n string. If the key doesn't resolve,
108    * then name is returned.if the key doesn't exist. Use this for
109    * programatically constructed keys that have have a human readable
110    * alternative used in the program (e.g. BLOSUM62 and label.score_blosum62)
111    * 
112    * @param keyroot
113    * @param name
114    * @return
115    */
116   public static String getStringOrReturn(String keyroot, String name)
117   {
118     String smkey = keyroot + name.toLowerCase().replaceAll(" ", "");
119     try
120     {
121       name = rb.getString(smkey);
122     } catch (Exception x)
123     {
124       log.finest("I18N missing key with root " + keyroot + ": " + loc + "\t"
125               + smkey);
126     }
127     return name;
128   }
129 }