1 package net.sf.openrocket.l10n;
3 import java.util.Locale;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
7 import net.sf.openrocket.logging.LogHelper;
8 import net.sf.openrocket.startup.Application;
11 * A translator that obtains translated strings from a resource bundle.
13 * If a message is not found in any resource bundle, an error is logged and the key itself
16 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
18 public class ResourceBundleTranslator implements Translator {
19 private static final LogHelper log = Application.getLogger();
21 private final ResourceBundle bundle;
22 private final String baseName;
23 private final Locale locale;
26 * Create a ResourceBundleTranslator using the default Locale.
28 * @param baseName the base name of the resource bundle
30 public ResourceBundleTranslator(String baseName) {
31 this(baseName, Locale.getDefault());
35 * Create a ResourceBundleTranslator using the specified Locale.
37 * @param baseName the base name of the resource bundle
38 * @param locale the locale to use
40 public ResourceBundleTranslator(String baseName, Locale locale) {
41 this.bundle = ResourceBundle.getBundle(baseName, locale);
42 this.baseName = baseName;
48 * NOTE: This method must be thread-safe!
51 public synchronized String get(String key) {
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);