Example usage for java.util ResourceBundle containsKey

List of usage examples for java.util ResourceBundle containsKey

Introduction

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

Prototype

public boolean containsKey(String key) 

Source Link

Document

Determines whether the given key is contained in this ResourceBundle or its parent bundles.

Usage

From source file:Main.java

public static void main(String[] args) {

    ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US);

    System.out.println(bundle.getString("hello"));

    System.out.println(bundle.containsKey("bye"));

    System.out.println(bundle.containsKey("hello"));

}

From source file:RBPropDemo.java

public static void main(String[] args) {
    ResourceBundle.clearCache();/*from  ww  w.j a  v a 2  s  .  c  om*/
    String bundleName = "myproj.MyResources";

    ResourceBundle myResources = ResourceBundle.getBundle(bundleName, Locale.GERMAN);

    System.out.println("Key's values:");
    System.out.println(myResources.getString("okKey"));
    System.out.println(myResources.getString("cancelKey"));
    System.out.println(myResources.getString("submitKey"));
    System.out.println("\nChecking okKey in resource bundle:");
    if (myResources.containsKey("okKey")) {
        System.out.println("okKey exists! " + " Value = " + myResources.getString("okKey"));
    } else {
        System.out.println("The key Doesn't Exist");
    }

    System.out.println("\nGet a set of keys:");
    Set<String> keySet = myResources.keySet();
    Object[] keys = keySet.toArray();
    for (int i = 0; i < keys.length; i++) {
        System.out.println("Key " + (i + 1) + " = " + keys[i]);
    }
}

From source file:com.htmlhifive.tools.codeassist.core.messages.MessagesBase.java

/**
 * ????????./*from w w w.  j  av a 2  s  . com*/
 * 
 * @param key ?
 * @return ????
 */
private static String getResourceBundleString(String key) {

    synchronized (RESOURCE_BUNDLES) {
        for (ResourceBundle resourceBundle : RESOURCE_BUNDLES) {
            if (resourceBundle.containsKey(key)) {
                return resourceBundle.getString(key);
            }
        }
        return null;
    }
}

From source file:de.elbe5.base.util.StringUtil.java

public static String getString(String key, Locale locale) {
    String s = "";
    try {// w w w .  j  av a2 s . c o m
        ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
        if (bundle == null || key == null || !bundle.containsKey(key)) {
            Log.warn("resource string not found for key " + key + " of locale " + locale);
            return "...";
        }
        s = bundle.getString(key);
    } catch (MissingResourceException ignore) {
    }
    if (s.isEmpty()) {
        Log.warn("resource string is empty for key " + key + " of locale " + locale);
        return "..";
    }
    return s;
}

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

/**
 * ?Properties? ,java.util.ResourceBundlegetBundle()??
 * /*from w  w  w. ja va2 s .  com*/
 * @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.discovery.darchrow.util.ResourceBundleUtil.java

/**
 * ?Properties? , {@link java.util.ResourceBundle#getBundle(String)} ??.
 * /*ww w  .j  a v  a  2 s  . c om*/
 * @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:com.sunchenbin.store.feilong.core.util.ResourceBundleUtil.java

/**
 * ?Properties? , {@link java.util.ResourceBundle#getBundle(String)} ??.
 * /*from w ww.j  av  a 2  s. co 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;
}

From source file:com.evolveum.midpoint.report.impl.ReportUtils.java

public static String getPropertyString(String key, String defaultValue) {
    String val = (defaultValue == null) ? key : defaultValue;
    ResourceBundle bundle;
    try {/*w ww  .j a  v  a  2 s  .  c o  m*/
        bundle = ResourceBundle.getBundle("localization/Midpoint", new Locale("en", "US"));
    } catch (MissingResourceException e) {
        return (defaultValue != null) ? defaultValue : key; //workaround for Jasper Studio
    }
    if (bundle != null && bundle.containsKey(key)) {
        val = bundle.getString(key);
    }
    return val;
}

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

/**
 * ?Properties? , {@link java.util.ResourceBundle#getBundle(String)} ??.
 * /*from w  ww.  j a v  a  2 s  .  c o  m*/
 * <p>
 * ????? <code>arguments</code> , {@link MessageFormatUtil#format(String, Object...)} ??
 * </p>
 * 
 * <h3>:</h3>
 * 
 * <blockquote>
 * 
 * <p>
 * ? messages\feilong-core-test.properties ,:
 * </p>
 * 
 * <pre class="code">
 * test.arguments=my name is {0},age is {1}
 * </pre>
 * 
 * :
 * 
 * <pre class="code">
 * ResourceBundleUtil.getValueWithArguments(resourceBundle, "test.arguments", "feilong", "18")
 * </pre>
 * 
 * <b>:</b>
 * 
 * <pre class="code">
 * my name is feilong,age is 18
 * </pre>
 * 
 * </blockquote>
 *
 * @param resourceBundle
 *            the resource bundle
 * @param key
 *            Properties???
 * @param arguments
 *            ?Object[]?
 * @return ?,
 *         <ul>
 *         <li>key?,LOGGER.warn ,? {@link StringUtils#EMPTY}</li>
 *         <li>key,valuenull  empty,value</li>
 *         </ul>
 * @throws NullPointerException
 *              <code>resourceBundle</code>  <code>key</code> null
 * @throws IllegalArgumentException
 *              <code>key</code> blank, {@link IllegalArgumentException}
 * @see java.util.ResourceBundle#getString(String)
 * @see MessageFormatUtil#format(String, Object...)
 * @since 1.8.1 support arguments param
 */
public static String getValue(ResourceBundle resourceBundle, String key, Object... arguments) {
    Validate.notNull(resourceBundle, "resourceBundle can't be null!");
    Validate.notBlank(key, "key can't be null/empty!");

    if (!resourceBundle.containsKey(key)) {
        LOGGER.warn("resourceBundle:[{}] don't containsKey:[{}]", resourceBundle, key);
        return EMPTY;
    }

    String value = resourceBundle.getString(key);
    if (isNullOrEmpty(value)) {
        LOGGER.trace("resourceBundle has key:[{}],but value is null/empty", key);
    }
    return isNullOrEmpty(value) ? EMPTY : MessageFormatUtil.format(value, arguments);// ? arguments null,
}

From source file:ca.uhn.fhir.i18n.HapiLocalizer.java

private String findFormatString(String theQualifiedKey) {
    String formatString = null;/*  w  w w . j a v a 2s.  com*/
    for (ResourceBundle nextBundle : myBundle) {
        if (nextBundle.containsKey(theQualifiedKey)) {
            formatString = nextBundle.getString(theQualifiedKey);
        }
        if (isNotBlank(formatString)) {
            break;
        }
    }

    if (formatString == null) {
        ourLog.warn("Unknown localization key: {}", theQualifiedKey);
        formatString = UNKNOWN_I18N_KEY_MESSAGE;
    }
    return formatString;
}