SafetyMutex and rocket optimization updates
[debian/openrocket] / src / net / sf / openrocket / l10n / ResourceBundleTranslator.java
1 package net.sf.openrocket.l10n;
2
3 import java.util.Locale;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6
7 import net.sf.openrocket.logging.LogHelper;
8 import net.sf.openrocket.startup.Application;
9
10 /**
11  * A translator that obtains translated strings from a resource bundle.
12  * <p>
13  * If a message is not found in any resource bundle, an error is logged and the key itself
14  * is returned.
15  * 
16  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
17  */
18 public class ResourceBundleTranslator implements Translator {
19         private static final LogHelper log = Application.getLogger();
20         
21         private final ResourceBundle bundle;
22         private final String baseName;
23         private final Locale locale;
24         
25         /**
26          * Create a ResourceBundleTranslator using the default Locale.
27          * 
28          * @param baseName      the base name of the resource bundle
29          */
30         public ResourceBundleTranslator(String baseName) {
31                 this(baseName, Locale.getDefault());
32         }
33         
34         /**
35          * Create a ResourceBundleTranslator using the specified Locale.
36          * 
37          * @param baseName      the base name of the resource bundle
38          * @param locale        the locale to use
39          */
40         public ResourceBundleTranslator(String baseName, Locale locale) {
41                 this.bundle = ResourceBundle.getBundle(baseName, locale);
42                 this.baseName = baseName;
43                 this.locale = locale;
44         }
45         
46         
47         /*
48          * NOTE:  This method must be thread-safe!
49          */
50         @Override
51         public synchronized String get(String key) {
52                 try {
53                         return bundle.getString(key);
54                 } catch (MissingResourceException e) {
55                         log.error("String not found for key '" + key + "' in bundle '" + baseName + "' with locale " + locale, e);
56                 }
57                 return key;
58         }
59         
60 }