1 package net.sf.openrocket.l10n;
3 import java.util.Locale;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
8 * A translator that obtains translated strings from a resource bundle.
10 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
12 public class ResourceBundleTranslator implements Translator {
14 private final ResourceBundle bundle;
15 private final ResourceBundle english;
18 * Create a ResourceBundleTranslator using the default Locale.
20 * @param baseName the base name of the resource bundle
22 public ResourceBundleTranslator(String baseName) {
23 this(baseName, Locale.getDefault());
27 * Create a ResourceBundleTranslator using the specified Locale.
29 * @param baseName the base name of the resource bundle
30 * @param locale the locale to use
32 public ResourceBundleTranslator(String baseName, Locale locale) {
33 this.bundle = ResourceBundle.getBundle(baseName, locale);
34 this.english = ResourceBundle.getBundle(baseName, Locale.ROOT);
39 * NOTE: This method must be thread-safe!
42 public synchronized String get(String key) {
43 return bundle.getString(key);
47 public synchronized String get(String base, String text) {
48 String key = base + "." + L10N.normalize(text);
50 return bundle.getString(key);
51 } catch (MissingResourceException e) {
57 public synchronized String getBaseText(String base, String translation) {
58 String prefix = base + ".";
59 for (String key : bundle.keySet()) {
60 if (key.startsWith(prefix)) {
61 String value = bundle.getString(key);
62 if (value.equals(translation)) {
63 return english.getString(key);