dd916b6cde240c9464ea57bee78be6b291981695
[debian/openrocket] / core / src / net / sf / openrocket / l10n / ExceptionSuppressingTranslator.java
1 package net.sf.openrocket.l10n;
2
3 import java.util.Locale;
4 import java.util.MissingResourceException;
5
6 import net.sf.openrocket.startup.Application;
7
8 /**
9  * A translator that suppresses MissingResourceExceptions and handles them gracefully.
10  * For the first missing key this class calls the exception handler, and afterwards
11  * always returns the key for missing translations.
12  * 
13  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14  */
15 public class ExceptionSuppressingTranslator implements Translator {
16         
17         static boolean errorReported = false;
18         
19         private final Translator translator;
20         
21         
22         /**
23          * Sole constructor.
24          * 
25          * @param translator    the translator to use
26          */
27         public ExceptionSuppressingTranslator(Translator translator) {
28                 this.translator = translator;
29         }
30         
31         
32
33         @Override
34         public String get(String key) {
35                 try {
36                         return translator.get(key);
37                 } catch (MissingResourceException e) {
38                         handleError(key, e);
39                 }
40                 
41                 return key;
42         }
43         
44         
45
46         private static synchronized void handleError(String key, MissingResourceException e) {
47                 if (!errorReported) {
48                         errorReported = true;
49                         Application.getExceptionHandler().handleErrorCondition("Can not find translation for '" + key + "' locale=" + Locale.getDefault(), e);
50                 }
51         }
52         
53 }