JAL-1517 fix copyright for 2.8.2
[jalview.git] / src / jalview / util / MessageManager.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.getLogger(MessageManager.class.getCanonicalName());
41         
42         private static Locale loc;
43         
44         
45         
46     static{
47       try {
48         /* Localize Java dialogs */
49         loc = Locale.getDefault();
50         // Locale.setDefault(loc);
51         /* Getting messages for GV */
52         log.info("Getting messages for lang: "+loc);
53         rb = ResourceBundle.getBundle("lang.Messages", loc);
54         if (log.isLoggable(Level.FINEST)) {
55           // this might take a while, so we only do it if it will be shown
56           log.finest("Language keys: "+rb.keySet());
57         }
58       } catch (Exception q) {
59         log.warning("Exception when initting Locale for i18n messages\n"+q.getMessage());
60         q.printStackTrace();
61       }
62       catch (Error v)
63       {
64         log.warning("Error when initting Locale for i18n messages\n"+v.getMessage());
65         v.printStackTrace();
66       }
67       
68      
69     }
70     
71     public static String getString(String key){
72         String value = "[missing key] " + key;
73         try{
74                 value = rb.getString(key);
75         }catch(Exception e){
76           log.warning("I18N missing: "+loc+"\t"+key);
77         }
78         return value;
79     }
80     
81         public static Locale getLocale() {
82                 return loc;
83         }
84         public static String formatMessage(String key, Object... params){
85                 return MessageFormat.format(rb.getString(key), (Object[]) params);
86         }
87
88   /**
89    * lookup and return a key given a root and a human-readable(ish) name that when combined might resolve to an i18n string.
90    * If the key doesn't resolve, then name is returned.if the key doesn't exist.
91    * Use this for programatically constructed keys that have have a human readable alternative used in the program (e.g. BLOSUM62 and label.score_blosum62) 
92    * @param keyroot
93    * @param name
94    * @return
95    */
96   public static String getStringOrReturn(String keyroot, String name)
97   {
98     String smkey = keyroot
99             + name.toLowerCase().replaceAll(" ", "");
100     try {
101       name = rb.getString(smkey); 
102     }
103     catch (Exception x) {
104       log.finest("I18N missing key with root "+keyroot+": "+loc+"\t"+smkey);
105     }
106     return name;
107   }
108 }