Example usage for java.util ResourceBundle getKeys

List of usage examples for java.util ResourceBundle getKeys

Introduction

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

Prototype

public abstract Enumeration<String> getKeys();

Source Link

Document

Returns an enumeration of the keys.

Usage

From source file:i5.las2peer.services.mobsos.SurveyService.java

/**
 * localizes the given String t according to locale l. If no resource bundle exists for locale l, fall back to English.
 * Input string t is expected to contain placeholders ${k}, where k is a key defined in the ResourceBundle.
 * //from ww w .j a v  a2  s.  c  o m
 * @param t a String to be localized
 * @param l a Locale
 * @return
 */
private String i18n(String t, String lang) {

    // now parse locales from accept-language header
    Pattern p = Pattern.compile("[a-z]+-[A-Z]+");
    Matcher m = p.matcher(lang);

    // do not iterate over all locales found, but only use first option with highest preference.

    Locale l = null;

    if (m.find()) {
        String[] tokens = m.group().split("-");
        l = new Locale(tokens[0], tokens[1]);
        //System.out.println("Locale: " + l.getDisplayCountry() + " " + l.getDisplayLanguage());
    }

    ResourceBundle messages = ResourceBundle.getBundle("MessageBundle", l);
    Enumeration<String> e = messages.getKeys();

    while (e.hasMoreElements()) {

        String key = e.nextElement();
        String translation = messages.getString(key);
        t = t.replaceAll("\\$\\{" + key + "\\}", escapeHtml4(translation));
    }

    return t;
}

From source file:org.regenstrief.util.Util.java

/**
 * Creates a HashMap containing the same data as the given ResourceBundle; can be useful because
 * HashMap's get() method is faster than ResourceBundle's getString() method when the key is not
 * found, because HashMap returns null, while ResourceBundle throws an Exception
 * //from   w  w  w. j  a va2 s .c o  m
 * @param rb the ResourceBundle
 * @return the HashMap
 **/
public final static HashMap<String, String> resourceBundleToHashMap(final ResourceBundle rb) {
    final Enumeration<?> e = rb.getKeys();
    final HashMap<String, String> hm = new HashMap<String, String>();

    while (e.hasMoreElements()) {
        final String key = e.nextElement().toString();
        hm.put(key, rb.getString(key));
    }

    return hm;
}