Material localization support
[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         @Override
46         public String get(String base, String text) {
47                 return translator.get(base, text);
48         }
49         
50         
51         @Override
52         public String getBaseText(String base, String translation) {
53                 return translator.getBaseText(base, translation);
54         }
55         
56         
57         private static synchronized void handleError(String key, MissingResourceException e) {
58                 if (!errorReported) {
59                         errorReported = true;
60                         Application.getExceptionHandler().handleErrorCondition("Can not find translation for '" + key + "' locale=" + Locale.getDefault(), e);
61                 }
62         }
63         
64         
65         
66 }