a4cedc05d0eb12d3cf590a4ad0a4876b13b83b3c
[debian/openrocket] / src / net / sf / openrocket / l10n / ExceptionSuppressingTranslator.java
1 package net.sf.openrocket.l10n;
2
3 import java.util.HashSet;
4 import java.util.Locale;
5 import java.util.MissingResourceException;
6 import java.util.Set;
7
8 import net.sf.openrocket.gui.main.ExceptionHandler;
9
10 /**
11  * A translator that suppresses MissingResourceExceptions and handles them gracefully.
12  * For every missing key this class calls the exception handler exactly once, and
13  * returns the key itself as the translation.
14  * 
15  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
16  */
17 public class ExceptionSuppressingTranslator implements Translator {
18         
19         private static final Set<String> errors = new HashSet<String>();
20         // For unit testing:
21         static int failures = 0;
22         
23         private final Translator translator;
24         
25         
26
27         /**
28          * Sole constructor.
29          * 
30          * @param translator    the translator to use
31          */
32         public ExceptionSuppressingTranslator(Translator translator) {
33                 this.translator = translator;
34         }
35         
36         
37
38         @Override
39         public String get(String key) {
40                 try {
41                         return translator.get(key);
42                 } catch (MissingResourceException e) {
43                         handleError(key, e);
44                 }
45                 
46                 return key;
47         }
48         
49         
50
51         private static synchronized void handleError(String key, MissingResourceException e) {
52                 if (errors.add(key)) {
53                         failures++;
54                         ExceptionHandler.handleErrorCondition("Can not find translation for '" + key + "' locale=" + Locale.getDefault(), e);
55                 }
56         }
57         
58 }