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:org.exoplatform.commons.notification.template.TemplateUtils.java

/**
 * Gets Resource Bundle value//from w  ww.  j a va 2 s. co  m
 * @param key
 * @param locale
 * @param resourcePath
 * @return
 */
public static String getResourceBundle(String key, Locale locale, String resourcePath) {
    if (key == null || key.trim().length() == 0) {
        return "";
    }
    if (locale == null || locale.getLanguage().isEmpty()) {
        locale = Locale.ENGLISH;
    }

    ResourceBundleService bundleService = CommonsUtils.getService(ResourceBundleService.class);
    ResourceBundle res = bundleService.getResourceBundle(resourcePath, locale);

    if (res == null || res.containsKey(key) == false) {
        LOG.warn("Resource Bundle key not found. " + key + " in source path: " + resourcePath);
        return key;
    }

    return res.getString(key);
}

From source file:org.jtalks.jcommune.plugin.auth.poulpe.service.PoulpeAuthService.java

/**
 * Parse error code with specified {@link ResourceBundle}.
 *
 * @param errorCode      error code/*from w w w.  j a v a2s  . c o  m*/
 * @param resourceBundle used {@link ResourceBundle}
 * @return parsed error as pair field - error message
 */
private Map.Entry<String, String> parseErrorCode(String errorCode, ResourceBundle resourceBundle) {
    Map.Entry<String, String> error = null;
    if (resourceBundle.containsKey(errorCode)) {
        String errorMessage = resourceBundle.getString(errorCode);
        if (errorCode.contains("email")) {
            errorMessage = errorMessage.replace("{max}",
                    String.valueOf(org.jtalks.common.model.entity.User.EMAIL_MAX_LENGTH));
            error = new HashMap.SimpleEntry<>("userDto.email", errorMessage);
        } else if (errorCode.contains("username")) {
            errorMessage = errorMessage
                    .replace("{min}", String.valueOf(org.jtalks.common.model.entity.User.USERNAME_MIN_LENGTH))
                    .replace("{max}", String.valueOf(org.jtalks.common.model.entity.User.USERNAME_MAX_LENGTH));
            error = new HashMap.SimpleEntry<>("userDto.username", errorMessage);
        } else if (errorCode.contains("password")) {
            errorMessage = errorMessage
                    .replace("{min}", String.valueOf(org.jtalks.common.model.entity.User.PASSWORD_MIN_LENGTH))
                    .replace("{max}", String.valueOf(org.jtalks.common.model.entity.User.PASSWORD_MAX_LENGTH));
            error = new HashMap.SimpleEntry<>("userDto.password", errorMessage);
        }
    }
    return error;
}

From source file:org.kalypso.commons.i18n.ResourceBundleUtils.java

/**
 * Returns an translated string for a given key, similar to i18n of the plugin.xml file.<br>
 * The string is translated according to the given properties, if it start with '%'. If not, the original value is
 * returned.<br>/* ww w . ja  v a  2 s .  c o m*/
 * If the given string starts with '%', but the bundle does not contain a corresponding entry, the original string is
 * returned.
 * 
 * @param translatableString
 *          If <code>null</code>, <code>null</code> is returned.
 * @param resourceBundle
 *          If <code>null</code>, we always return <code>translatableString</code>.
 * @return If <code>translatableString</code> starts with '%', <code>resourceBundle.getString( translatableString.substring(1) )</code> is returned. Else return <code>translatableString</code>.
 */
public static String getI18NString(final String translatableString, final ResourceBundle resourceBundle) {
    if (translatableString == null)
        return null;

    if (translatableString.isEmpty())
        return translatableString;

    if (resourceBundle == null)
        return translatableString;

    if (translatableString.charAt(0) == '%') {
        final String key = translatableString.substring(1);
        if (resourceBundle.containsKey(key)) {
            final String string = resourceBundle.getString(key);
            if (string != null)
                return string;
        }
    }

    return translatableString;
}

From source file:org.kalypso.commons.i18n.ResourceBundleUtils.java

/**
 * Returns <code>bundle.getString(key)</code>, silently returning <code>null</code>, if key is not known.
 *//*from w ww  . j  av  a2 s .  c om*/
public static String getStringQuiet(final ResourceBundle bundle, final String key) {
    if (bundle != null && bundle.containsKey(key))
        return bundle.getString(key);

    return null;
}

From source file:org.kuali.rice.krad.messages.providers.ResourceMessageProvider.java

/**
 * Iterates through the resource bundles for the give namespace (or the application if namespace is not given)
 * and finds the message that matches the given key
 *
 * <p>//from  w ww  .  j  av  a  2s  . com
 * If the message is found in more than one bundle, the text from the bundle that is loaded last will be used
 * </p>
 *
 * <p>
 * If the given component code is the default component, resource keys that do not have a component defined
 * and match the given key will also be considered matches
 * </p>
 *
 * @see org.kuali.rice.krad.messages.MessageProvider#getMessage(java.lang.String, java.lang.String,
 *      java.lang.String, java.lang.String)
 */
public Message getMessage(String namespace, String component, String key, String locale) {
    Message message = null;

    List<ResourceBundle> bundles = getCachedResourceBundles(namespace, locale);

    // iterate through bundles and find message text, if more than one bundle contains the message
    // the last one iterated over will be used
    String messageText = null;
    for (ResourceBundle bundle : bundles) {
        // check for key with component first
        String resourceKey = COMPONENT_PLACEHOLDER_BEGIN + component + COMPONENT_PLACEHOLDER_END + key;
        if (bundle.containsKey(resourceKey)) {
            messageText = bundle.getString(resourceKey);
        }
        // if component is default then check for key without component code
        else if (MessageService.DEFAULT_COMPONENT_CODE.equals(component) && bundle.containsKey(key)) {
            messageText = bundle.getString(key);
        }
    }

    // if message text was found build message object
    if (StringUtils.isNotBlank(messageText)) {
        message = buildMessage(namespace, component, key, messageText, locale);
    }

    return message;
}

From source file:org.phenotips.measurements.internal.DefaultMeasurementsChartConfigurationsFactory.java

@Override
public List<MeasurementsChartConfiguration> loadConfigurationsForMeasurementType(String measurementType) {
    ResourceBundle configuration = ResourceBundle.getBundle("measurementsChartsConfigurations");
    String key = "charts." + measurementType + ".configurations";
    if (!configuration.containsKey(key)) {
        return Collections.emptyList();
    }//w  w  w.  j  a v a2s  . co  m
    String[] charts = configuration.getString(key).split(",");
    List<MeasurementsChartConfiguration> result = new ArrayList<>(charts.length);
    for (String chart : charts) {
        SimpleMeasurementsChartConfiguration chartSettings = loadChart(key + '.' + chart + '.', configuration);
        if (validateChart(chartSettings)) {
            chartSettings.measurementType = measurementType;
            result.add(chartSettings);
        }
    }
    return result;
}

From source file:org.processbase.ui.core.ProcessbaseApplication.java

public static String getString(String key) {
    ResourceBundle messages = getCurrent().getPbMessages();
    if (messages == null || messages.containsKey(key) == false)
        return key;
    return messages.getString(key);
}

From source file:org.processbase.ui.core.ProcessbaseApplication.java

/**
 * Translate using resources/* w  ww  .  j  ava  2 s.c  o  m*/
 * @param key
 * @param defaultValue if resource not found
 * @return translated value
 */
public static String getString(String key, String defaultValue) {
    ResourceBundle messages = getCurrent().getPbMessages();
    if (messages == null || messages.containsKey(key) == false)
        return defaultValue;
    return messages.getString(key);
}

From source file:org.sonar.plsqlopen.CustomAnnotationBasedRulesDefinition.java

private void setupExternalNames(Collection<NewRule> rules) {
    ResourceBundle bundle = ResourceBundle.getBundle("org.sonar.l10n." + languageKey, locale);
    for (NewRule rule : rules) {
        String baseKey = "rule." + repository.key() + "." + rule.key();
        String nameKey = baseKey + ".name";
        if (bundle.containsKey(nameKey)) {
            rule.setName(bundle.getString(nameKey));
        }/* www .j  a va 2s  .c om*/
        for (NewParam param : rule.params()) {
            String paramDescriptionKey = baseKey + ".param." + param.key();
            if (bundle.containsKey(paramDescriptionKey)) {
                param.setDescription(bundle.getString(paramDescriptionKey));
            }
        }
    }
}

From source file:org.sonar.squidbridge.annotations.AnnotationBasedRulesDefinition.java

private void setupExternalNames(Collection<NewRule> rules) {
    URL resource = AnnotationBasedRulesDefinition.class
            .getResource("/org/sonar/l10n/" + languageKey + ".properties");
    if (resource == null) {
        return;/*from ww w .  j a  v  a  2s.  c  o m*/
    }
    ResourceBundle bundle = ResourceBundle.getBundle("org.sonar.l10n." + languageKey, Locale.ENGLISH);
    for (NewRule rule : rules) {
        String baseKey = "rule." + repository.key() + "." + rule.key();
        String nameKey = baseKey + ".name";
        if (bundle.containsKey(nameKey)) {
            rule.setName(bundle.getString(nameKey));
        }
        for (NewParam param : rule.params()) {
            String paramDescriptionKey = baseKey + ".param." + param.key();
            if (bundle.containsKey(paramDescriptionKey)) {
                param.setDescription(bundle.getString(paramDescriptionKey));
            }
        }
    }
}