Example usage for java.util ResourceBundle getString

List of usage examples for java.util ResourceBundle getString

Introduction

In this page you can find the example usage for java.util ResourceBundle getString.

Prototype

public final String getString(String key) 

Source Link

Document

Gets a string for the given key from this resource bundle or one of its parents.

Usage

From source file:org.jamwiki.utils.Utilities.java

/**
 * Given a message key and locale return a locale-specific message.
 *
 * @param key The message key that corresponds to the formatted message
 *  being retrieved./*from ww w.  j a v a2  s.c o m*/
 * @param locale The locale for the message that is to be retrieved.
 * @return A formatted message string that is specific to the locale.
 */
public static String formatMessage(String key, Locale locale) {
    ResourceBundle messages = ResourceBundle.getBundle("ApplicationResources", locale);
    return messages.getString(key);
}

From source file:com.amalto.workbench.compare.Utilities.java

public static String getString(ResourceBundle bundle, String key, String dfltValue) {

    if (bundle != null) {
        try {//from  ww  w .  ja  v  a2  s. c  o m
            return bundle.getString(key);
        } catch (MissingResourceException x) {
            // fall through
        }
    }
    return dfltValue;
}

From source file:com.feilong.commons.core.configure.ResourceBundleUtil.java

/**
 * ?Properties? ,java.util.ResourceBundlegetBundle()??
 * /*from w w  w  . j av  a2  s .  co m*/
 * @param resourceBundle
 *            ?+??(??)
 * @param key
 *            Properties???
 * @return <br>
 *         ?,
 *         <ul>
 *         <li>key?,log.warn ,?null</li>
 *         <li>key,valuenull  empty,log.warn ,?value</li>
 *         </ul>
 * @see java.util.ResourceBundle#getString(String)
 */
public static String getValue(ResourceBundle resourceBundle, String key) {
    if (!resourceBundle.containsKey(key)) {
        log.warn("resourceBundle don't containsKey:[{}]", key);
    } else {
        try {
            String value = resourceBundle.getString(key);
            if (Validator.isNullOrEmpty(value)) {
                log.warn("resourceBundle has key:[{}],but value is null/empty", key);
            }
            return value;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    return null;
}

From source file:com.examples.with.different.packagename.concolic.MathRuntimeException.java

/**
 * Translate a string to a given locale.
 * @param s string to translate/*from  w  w w . j a v  a2 s  .  com*/
 * @param locale locale into which to translate the string
 * @return translated string or original string
 * for unsupported locales or unknown strings
 */
private static String translate(final String s, final Locale locale) {
    try {
        ResourceBundle bundle = ResourceBundle.getBundle("org.apache.commons.math.MessagesResources", locale);
        if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
            // the value of the resource is the translated string
            return bundle.getString(s);
        }

    } catch (MissingResourceException mre) {
        // do nothing here
    }

    // the locale is not supported or the resource is unknown
    // don't translate and fall back to using the string as is
    return s;

}

From source file:at.alladin.rmbt.shared.Helperfunctions.java

public static String getRoamingType(final ResourceBundle labels, final int roamingType) {
    final String roamingValue;
    switch (roamingType) {
    case 0://from ww w.j  ava2s .com
        roamingValue = labels.getString("value_roaming_none");
        break;
    case 1:
        roamingValue = labels.getString("value_roaming_national");
        break;
    case 2:
        roamingValue = labels.getString("value_roaming_international");
        break;
    default:
        roamingValue = "?";
        break;
    }
    return roamingValue;
}

From source file:net.sf.eclipsecs.core.config.meta.MetadataFactory.java

private static String localize(String localizationCandidate, ResourceBundle metadataBundle) {

    if (metadataBundle != null && localizationCandidate != null && localizationCandidate.startsWith("%")) {
        try {/* ww  w .ja  va  2  s  . co m*/
            return metadataBundle.getString(localizationCandidate.substring(1));
        } catch (MissingResourceException e) {
            return localizationCandidate;
        }
    }
    return localizationCandidate;
}

From source file:com.discovery.darchrow.util.ResourceBundleUtil.java

/**
 * ?Properties? , {@link java.util.ResourceBundle#getBundle(String)} ??.
 * //from   w  ww.j  a  v  a  2s.  c o m
 * @param resourceBundle
 *            ?+??(??)
 * @param key
 *            Properties???
 * @return <br>
 *         ?,
 *         <ul>
 *         <li>key?,LOGGER.warn ,?null</li>
 *         <li>key,valuenull  empty,LOGGER.warn ,?value</li>
 *         </ul>
 * @see java.util.ResourceBundle#getString(String)
 */
public static String getValue(ResourceBundle resourceBundle, String key) {
    if (!resourceBundle.containsKey(key)) {
        LOGGER.warn("resourceBundle:[{}] don't containsKey:[{}]", resourceBundle, key);
    } else {
        try {
            String value = resourceBundle.getString(key);
            if (Validator.isNullOrEmpty(value)) {
                LOGGER.warn("resourceBundle has key:[{}],but value is null/empty", key);
            }
            return value;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    return null;
}

From source file:I18NUtil.java

/**
 * Get the messages for a locale./* w  w w.  j  a va2  s  . c o  m*/
 * <p>
 * Will use cache where available otherwise will load into cache from bundles.
 * 
 * @param locale    the locale
 * @return          message map
 */
private static Map<String, String> getLocaleProperties(Locale locale) {
    Set<String> loadedBundles = null;
    Map<String, String> props = null;
    int loadedBundleCount = 0;
    try {
        readLock.lock();
        loadedBundles = loadedResourceBundles.get(locale);
        props = cachedMessages.get(locale);
        loadedBundleCount = resouceBundleBaseNames.size();
    } finally {
        readLock.unlock();
    }

    if (loadedBundles == null) {
        try {
            writeLock.lock();
            loadedBundles = new HashSet<String>();
            loadedResourceBundles.put(locale, loadedBundles);
        } finally {
            writeLock.unlock();
        }
    }

    if (props == null) {
        try {
            writeLock.lock();
            props = new HashMap<String, String>();
            cachedMessages.put(locale, props);
        } finally {
            writeLock.unlock();
        }
    }

    if (loadedBundles.size() != loadedBundleCount) {
        try {
            writeLock.lock();
            for (String resourceBundleBaseName : resouceBundleBaseNames) {
                if (loadedBundles.contains(resourceBundleBaseName) == false) {
                    ResourceBundle resourcebundle = ResourceBundle.getBundle(resourceBundleBaseName, locale);
                    Enumeration<String> enumKeys = resourcebundle.getKeys();
                    while (enumKeys.hasMoreElements() == true) {
                        String key = enumKeys.nextElement();
                        props.put(key, resourcebundle.getString(key));
                    }
                    loadedBundles.add(resourceBundleBaseName);
                }
            }
        } finally {
            writeLock.unlock();
        }
    }

    return props;
}

From source file:com.clustercontrol.util.Messages.java

/**
 * Returns the resource object with the given key in the resource bundle. If
 * there isn't any value under the given key, the default value is returned.
 * /*from w  w w  .j a v a  2 s . com*/
 * @param key
 *            the resource name
 * @param def
 *            the default value
 * @param locale
 * @return the string
 */
public static String getString(String key, String def, Locale locale) {
    String ret = def;
    ResourceBundle.Control control = ResourceBundle.Control
            .getNoFallbackControl(ResourceBundle.Control.FORMAT_DEFAULT);
    if (clientFlag) {
        ResourceBundle bundle = null;
        try {
            if (locale != null) {
                bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_CLIENT, locale, control);
            } else {
                bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_CLIENT, control);
            }
        } catch (MissingResourceException e) {
            m_log.info(RESOURCE_BUNDLE_CLIENT + " missing"); // Client???1??
            clientFlag = false;
        }
        try {
            if (clientFlag) {
                ret = bundle.getString(key);
            }
        } catch (MissingResourceException e) {
        }
    }
    if (commonFlag) {
        ResourceBundle bundle = null;
        try {
            if (locale != null) {
                bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_COMMON, locale, control);
            } else {
                bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_COMMON, control);
            }
        } catch (MissingResourceException e) {
            m_log.warn(RESOURCE_BUNDLE_COMMON + " missing"); // ???????
            commonFlag = false;
        }
        try {
            if (commonFlag) {
                ret = bundle.getString(key);
            }
        } catch (MissingResourceException e) {
        }
    }

    return ret;
}

From source file:com.sunchenbin.store.feilong.core.util.ResourceBundleUtil.java

/**
 * ?Properties? , {@link java.util.ResourceBundle#getBundle(String)} ??.
 * /*from  ww  w .j  a  va 2  s.  c o  m*/
 * @param resourceBundle
 *            ?+??(??)
 * @param key
 *            Properties???
 * @return <br>
 *         ?,
 *         <ul>
 *         <li>key?,LOGGER.warn ,?null</li>
 *         <li>key,valuenull  empty,LOGGER.warn ,?value</li>
 *         </ul>
 * @see java.util.ResourceBundle#getString(String)
 */
public static String getValue(ResourceBundle resourceBundle, String key) {
    if (!resourceBundle.containsKey(key)) {
        LOGGER.debug("resourceBundle:[{}] don't containsKey:[{}]", resourceBundle, key);
        return StringUtils.EMPTY;
    }

    try {
        String value = resourceBundle.getString(key);
        if (Validator.isNullOrEmpty(value)) {
            LOGGER.debug("resourceBundle has key:[{}],but value is null/empty", key);
        }
        return value;
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return StringUtils.EMPTY;
}