880252e0a64c7419107737cd0b55d58043aeb48c
[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.HashSet;
25 import java.util.Locale;
26 import java.util.ResourceBundle;
27 import java.util.ResourceBundle.Control;
28 import java.util.Set;
29
30 import org.apache.log4j.Logger;
31
32 /**
33  * 
34  * @author David Roldan Martinez
35  * @author Thomas Abeel
36  * 
37  * 
38  */
39 public class MessageManager
40 {
41
42   // BH 2018 switched to org.apache.llog4j.Logger
43
44   private static ResourceBundle rb;
45
46   private final static Logger log = Logger
47           .getLogger(MessageManager.class.getCanonicalName());
48
49   private final static Locale loc;
50
51   private final static Set<String> reportedMissing = new HashSet<>();
52
53   static
54   {
55     loc = Locale.getDefault();
56     try
57     {
58       /* Getting messages for GV */
59       // BH 2020.0-6.05 using new Locale("") to avoid unnecessary check for "en"
60       // here; works in Java as well, but not implementing that.
61       log.info("Getting messages for lang: " + loc);
62       Control control = Control.getControl(Control.FORMAT_PROPERTIES);
63       rb = ResourceBundle.getBundle("lang.Messages",
64               Platform.getLocaleOrNone(loc),
65               control);
66       // if (log.isLoggable(Level.FINEST))
67       // {
68       // // this might take a while, so we only do it if it will be shown
69       // log.info("Language keys: " + rb.keySet()); // was FINEST
70       // }
71     } catch (Exception q)
72     {
73       log.warn("Exception when initting Locale for i18n messages\n"
74               + q.getMessage());
75       q.printStackTrace();
76     } catch (Error v)
77     {
78       log.warn("Error when initting Locale for i18n messages\n"
79               + v.getMessage());
80       v.printStackTrace();
81     }
82
83   }
84
85   public static String getString(String key)
86   {
87     String value = "[missing key] " + key;
88     try
89     {
90       value = rb.getString(key);
91     } catch (Exception e)
92     {
93       String msg = "I18N missing: " + loc + "\t" + key;
94           logWarning(key, msg);
95     }
96     return value;
97   }
98
99   public static Locale getLocale()
100   {
101     return loc;
102   }
103
104   public static String formatMessage(String key, Object... params)
105   {
106     return MessageFormat.format(getString(key), params);
107   }
108
109   public static String formatMessage(String key, String[] params)
110   {
111     return MessageFormat.format(getString(key), (Object[]) params);
112   }
113
114   /**
115    * Looks up and returns a key given a root and a human-readable(ish) name that
116    * when combined might resolve to an i18n string. If the key doesn't resolve,
117    * then name is returned. Use this for programmatically constructed keys that
118    * have a human readable alternative used in the program (e.g. BLOSUM62 and
119    * label.score_blosum62).
120    * 
121    * @param keyroot
122    * @param name
123    * @return
124    */
125   public static String getStringOrReturn(String keyroot, String name)
126   {
127     String smkey = keyroot + name.toLowerCase().replaceAll(" ", "");
128     try
129     {
130       name = rb.getString(smkey);
131     } catch (Exception x)
132     {
133       String msg = "I18N missing key with root " + keyroot + ": " + loc + "\t"
134                           + smkey;
135           logWarning(smkey, msg);
136     }
137     return name;
138   }
139
140   /**
141    * Logs missing keys (each key once only per run)
142    * 
143    * @param key
144    * @param msg
145    */
146   private static void logWarning(String key, String msg) 
147   {
148         if (!reportedMissing.contains(key))
149         {
150       reportedMissing.add(key);
151           log.info(msg);
152         }
153   }
154 }