| 1 | package com.googlecode.lanterna.bundle; |
| 2 | |
| 3 | import java.security.PrivilegedAction; |
| 4 | import java.text.MessageFormat; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.Locale; |
| 7 | import java.util.Map; |
| 8 | import java.util.ResourceBundle; |
| 9 | |
| 10 | /** |
| 11 | * This class permits to deal easily with bundles. |
| 12 | * @author silveryocha |
| 13 | */ |
| 14 | public abstract class BundleLocator { |
| 15 | |
| 16 | private final String bundleName; |
| 17 | private static final ClassLoader loader = BundleLocator.class.getClassLoader(); |
| 18 | |
| 19 | /** |
| 20 | * Hidden constructor. |
| 21 | * @param bundleName the name of the bundle. |
| 22 | */ |
| 23 | protected BundleLocator(final String bundleName) { |
| 24 | this.bundleName = bundleName; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Method that centralizes the way to get the value associated to a bundle key. |
| 29 | * @param locale the locale. |
| 30 | * @param key the key searched for. |
| 31 | * @param parameters the parameters to apply to the value associated to the key. |
| 32 | * @return the formatted value associated to the given key. Empty string if no value exists for |
| 33 | * the given key. |
| 34 | */ |
| 35 | protected String getBundleKeyValue(Locale locale, String key, Object... parameters) { |
| 36 | String value = null; |
| 37 | try { |
| 38 | value = getBundle(locale).getString(key); |
| 39 | } catch (Exception ignore) { |
| 40 | } |
| 41 | return value != null ? MessageFormat.format(value, parameters) : null; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Gets the right bundle.<br/> |
| 46 | * A cache is handled as well as the concurrent accesses. |
| 47 | * @param locale the locale. |
| 48 | * @return the instance of the bundle. |
| 49 | */ |
| 50 | private ResourceBundle getBundle(Locale locale) { |
| 51 | return ResourceBundle.getBundle(bundleName, locale, loader); |
| 52 | } |
| 53 | } |