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:dk.netarkivet.common.utils.I18n.java

/**
 * Get a localized message for a given resource bundle, locale and label.
 *
 * In contrast to {@link java.util.ResourceBundle#getString}, this method is
 * forgiving on errors//  w  w  w  .  ja  v a 2s. c  o m
 *
 * I18n.getString("dk.netarkivet.common.Translations",
 * Locale.default, "job.unknown.id", 17)
 *
 * @param bundleName The name of the resource bundle, fully qualified, but
 * without the properties.
 * See {@link java.util.ResourceBundle#getBundle(String)}
 * @param locale The locale to get the string for
 * @param label The label of the string in the resource bundle
 * @param args Any args required for formatting the label
 * @return The localised string, or the label if the string could not be
 * found or the format is invalid or does not match the args.
 * @throws ArgumentNotValid on null bundleName, locale or label.
 */
public static String getString(String bundleName, Locale locale, String label, Object... args) {
    ArgumentNotValid.checkNotNullOrEmpty(bundleName, "String bundleName");
    ArgumentNotValid.checkNotNull(locale, "Locale locale");
    ArgumentNotValid.checkNotNullOrEmpty(label, "String label");
    try {
        ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
        String message = bundle.getString(label);
        try {
            return new MessageFormat(message, locale).format(args);
        } catch (IllegalArgumentException e) {
            log.warn("I18n bundle '" + bundleName + "' has wrong format '" + message + "' for label '" + label
                    + "'", e);
            return label;
        }
    } catch (MissingResourceException e) {
        log.warn("I18n bundle '" + bundleName + "' is missing label '" + label + "'", e);
        return label;
    }
}

From source file:fr.mael.microrss.util.Tools.java

public static String getMessage(String message, Locale locale) {
    ResourceBundle bundle = ResourceBundle.getBundle("fr.mael.microrss.i18n.messages", locale);
    return bundle.getString(message);
}

From source file:com.gcrm.util.security.UserUtil.java

public static void permissionCheck(String fieldName) throws Exception {
    User loginUser = UserUtil.getLoginUser();
    Integer value = (Integer) BeanUtil.getFieldValue(loginUser, fieldName);
    if (value != Role.ALL_OR_ENABLED) {
        ResourceBundle rb = CommonUtil.getResourceBundle();
        String errorMessage = rb.getString("access.nopermission");
        throw new AccessDeniedException(errorMessage);
    }/*w  ww.  j av  a  2s  .  c  o  m*/
}

From source file:com.gcrm.util.security.UserUtil.java

public static void scopeCheck(BaseEntity entity, String fieldName) throws Exception {
    User loginUser = UserUtil.getLoginUser();
    Integer value = (Integer) BeanUtil.getFieldValue(loginUser, fieldName);
    if (value == Role.OWNER_OR_DISABLED) {
        if (loginUser.getId().intValue() != entity.getOwner().getId().intValue()) {
            ResourceBundle rb = CommonUtil.getResourceBundle();
            String errorMessage = rb.getString("access.nopermission.record");
            throw new AccessDeniedException(errorMessage);
        }/*ww w. j  ava 2s  .  c  o m*/
    }
}

From source file:Main.java

static Properties convertResourceBundleToProperties(ResourceBundle resource) {
    Properties properties = new Properties();

    Enumeration<String> keys = resource.getKeys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        properties.put(key, resource.getString(key));
    }/*  w ww .  j a va2s .c o m*/

    return properties;
}

From source file:com.thomaskuenneth.openweathermapweather.WeatherUtils.java

private static String getAPIKey() {
    ResourceBundle bundle = ResourceBundle.getBundle("com.thomaskuenneth.openweathermapweather.api_key");
    return bundle.getString("api_key");
}

From source file:io.github.medinam.jcheesum.util.VersionChecker.java

public static void showDownloadLatestAlert() {
    Alert alert = new Alert(Alert.AlertType.INFORMATION);

    ResourceBundle resources = ResourceBundle.getBundle("i18n/Bundle", Locale.getDefault());

    alert.setTitle(resources.getString("newVersion"));
    alert.setHeaderText(resources.getString("newVersionLooksLike"));
    alert.setContentText(resources.getString("youHave") + App.class.getPackage().getImplementationVersion()
            + " " + resources.getString("latestVersionIs") + getLatestStable() + "\n\n"
            + resources.getString("wannaDownload"));

    ButtonType yesBtn = new ButtonType(resources.getString("yes"));
    ButtonType noBtn = new ButtonType(resources.getString("no"), ButtonBar.ButtonData.CANCEL_CLOSE);

    alert.getButtonTypes().setAll(yesBtn, noBtn);

    Optional<ButtonType> o = alert.showAndWait();

    if (o.get() == yesBtn) {
        General.openLink(getLatestStableDownloadURL());
    } else if (o.get() == noBtn) {
        alert.close();/*from www .  j  a  v a  2 s  .  c  o m*/
    }
}

From source file:com.eryansky.common.utils.io.ResourceUtils.java

/**
 *  {res}.properties  key //from  w w  w  . j a v  a2 s .c  om
 * 
 * @param locale
 * @param baseName
 * @param key
 * @return
 */
private static String _getStringForLocale(Locale locale, String baseName, String key) {
    try {
        ResourceBundle rb = ResourceBundle.getBundle(baseName, locale, ResourceUtils.class.getClassLoader());
        return (rb != null) ? rb.getString(key) : null;
    } catch (MissingResourceException e) {
        return null;
    } catch (NullPointerException e) {
        return null;
    }
}

From source file:org.blockfreie.element.hydrogen.murikate.ApplicationUtil.java

/**
 * Returns a message bundle and uses the argument to format. the string
 * //from   w w w .ja va  2s .  c om
 * @param bundleKey
 *            key to retrive in the bundle
 * @param args
 *            to format into string
 * @return the formated resource bundle property
 */
public static String messageFormat(String bundleKey, Object... args) {
    ResourceBundle bundle = messageBundle();
    String format = bundle.getString(bundleKey);
    String result = String.format(format, args);
    return elExpand(result);
}

From source file:com.puppycrawl.tools.checkstyle.utils.TokenUtils.java

/**
 * Returns the short description of a token for a given name.
 * @param name the name of the token ID to get
 * @return a short description/*  www . j  av  a 2s  .c  o m*/
 */
public static String getShortDescription(String name) {
    if (!TOKEN_NAME_TO_VALUE.containsKey(name)) {
        throw new IllegalArgumentException(TOKEN_NAME_EXCEPTION_PREFIX + name);
    }

    final String tokenTypes = "com.puppycrawl.tools.checkstyle.api.tokentypes";
    final ResourceBundle bundle = ResourceBundle.getBundle(tokenTypes);
    return bundle.getString(name);
}