Clover icon

Coverage Report

  1. Project Clover database Mon Sep 2 2024 17:57:51 BST
  2. Package jalview.util

File MessageManager.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
13% of files have more coverage

Code metrics

2
33
7
1
189
94
13
0.39
4.71
7
1.86

Classes

Class Line # Actions
MessageManager 39 33 13
0.904761990.5%
 

Contributing tests

This file is covered by 397 tests. .

Source view

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 jalview.bin.Console;
31   
32    /**
33    *
34    * @author David Roldan Martinez
35    * @author Thomas Abeel
36    *
37    *
38    */
 
39    public class MessageManager
40    {
41    private static ResourceBundle rb;
42   
43    private static Locale loc;
44   
45    private static Set<String> reportedMissing = new HashSet<>();
46   
 
47  54 toggle static
48    {
49  54 try
50    {
51    /* Localize Java dialogs */
52  54 loc = Locale.getDefault();
53    // Locale.setDefault(loc);
54    /* Getting messages for GV */
55  54 Console.info("Getting messages for lang: " + loc);
56  54 Control control = Control.getControl(Control.FORMAT_PROPERTIES);
57  54 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.info("Language keys: " + rb.keySet()); // was FINEST
62    // }
63    } catch (Exception q)
64    {
65  0 Console.warn("Exception when initting Locale for i18n messages\n"
66    + q.getMessage());
67  0 q.printStackTrace();
68    } catch (Error v)
69    {
70  0 Console.warn("Error when initting Locale for i18n messages\n"
71    + v.getMessage());
72  0 v.printStackTrace();
73    }
74   
75    }
76   
77    /**
78    * Returns the resource bundle text for the given key, or if not found, the
79    * key prefixed by "[missing key]"
80    *
81    * @param key
82    * @return
83    */
 
84  329892 toggle public static String getString(String key)
85    {
86  329891 String value = "[missing key] " + key;
87  329888 try
88    {
89  329887 value = rb.getString(key);
90    } catch (Exception e)
91    {
92  1 String msg = "I18N missing: " + loc + "\t" + key;
93  1 logWarning(key, msg);
94    }
95  329888 return value;
96    }
97   
 
98  1710 toggle public static Locale getLocale()
99    {
100  1710 return loc;
101    }
102   
103    /**
104    * Returns the resource bundle text for the given key, with tokens {@code {0},
105    * {1} etc replaced by the supplied parameters. If the key is not found,
106    * returns the key and values prefixed by "[missing key]"
107    *
108    * @param key
109    *
110    * @return
111    */
 
112  63192 toggle public static String formatMessage(String key, Object... params)
113    {
114  63192 try
115    {
116  63192 return MessageFormat.format(rb.getString(key), params);
117    } catch (Exception e)
118    {
119  1 Console.warn("I18N missing: " + loc + "\t" + key);
120    }
121  1 String value = "[missing key] " + key + "";
122  1 for (Object p : params)
123    {
124  2 value += " '" + p.toString() + "'";
125    }
126  1 return value;
127    }
128   
129    /**
130    * Returns the resource bundle text for the given key, with tokens {@code {0},
131    * {1} etc replaced by the supplied parameters. If the key is not found,
132    * returns the key and values prefixed by "[missing key]"
133    *
134    * @param key
135    *
136    * @return
137    */
 
138  40931 toggle public static String formatMessage(String key, String[] params)
139    {
140  40931 return formatMessage(key, (Object[]) params);
141    }
142   
143    /**
144    * Returns resource bundle text given a root and a human-readable(ish) name
145    * that when combined might resolve to an i18n string. {@code name} is forced
146    * to lower case, with any spaces removed, and concatenated to {@code keyroot}
147    * to form a lookup key.
148    * <p>
149    * If the key doesn't resolve, then {@code name} is returned.
150    * <p>
151    * Use this for programmatically constructed keys that might have a human
152    * readable alternative used in the program (e.g. BLOSUM62 and
153    * label.score_blosum62).
154    *
155    * @param keyroot
156    * @param name
157    * @return
158    */
 
159  10235 toggle public static String getStringOrReturn(String keyroot, String name)
160    {
161  10235 String smkey = keyroot
162    + name.toLowerCase(Locale.ROOT).replaceAll(" ", "");
163  10235 try
164    {
165  10235 name = rb.getString(smkey);
166    } catch (Exception x)
167    {
168  321 String msg = "I18N missing key with root " + keyroot + ": " + loc
169    + "\t" + smkey;
170  321 logWarning(smkey, msg);
171    }
172  10235 return name;
173    }
174   
175    /**
176    * Logs missing keys (each key once only per run)
177    *
178    * @param key
179    * @param msg
180    */
 
181  322 toggle private static void logWarning(String key, String msg)
182    {
183  322 if (!reportedMissing.contains(key))
184    {
185  4 reportedMissing.add(key);
186  4 Console.info(msg);
187    }
188    }
189    }