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:org.yccheok.jstock.gui.JTableUtilities.java

/**
 * Get the keys for a given string and locale.
 *
 * @param string the string for the desired key
 * @param locale the locale for the desired key
 * @return the keys for a given string and locale
 *///from w  w  w .jav  a 2 s . co  m
private static java.util.List<String> getKeys(String string, Locale locale) {
    if (string2KeyMap.containsKey(locale)) {
        final Map<String, java.util.List<String>> string2Key = string2KeyMap.get(locale);
        final java.util.List<String> result = string2Key.get(string);
        if (result == null) {
            return java.util.Collections.EMPTY_LIST;
        }
        return result;
    }

    final Map<String, java.util.List<String>> string2Key = new HashMap<String, java.util.List<String>>();

    // Ensure correct resource file is being loaded.
    // When ResourceBundle.getBundle(..., locale) is being called, the
    // system will try to search in the following sequence.
    // 1. gui_<locale>.properties.
    // 2. gui_<default_locale>.properties.
    // 3. gui.properties.
    final Locale oldLocale = Locale.getDefault();
    Locale.setDefault(locale);
    try {
        final ResourceBundle bundle = ResourceBundle.getBundle("org.yccheok.jstock.data.gui", locale);

        final Enumeration<String> enumeration = bundle.getKeys();
        while (enumeration.hasMoreElements()) {
            final String key = enumeration.nextElement();
            final String str = bundle.getString(key);
            java.util.List list = string2Key.get(str);
            if (list == null) {
                list = new ArrayList<String>();
                string2Key.put(str, list);
            }
            list.add(key);
        }

        string2KeyMap.put(locale, string2Key);
    } finally {
        Locale.setDefault(oldLocale);
    }

    final java.util.List<String> result = string2Key.get(string);
    if (result == null) {
        return java.util.Collections.EMPTY_LIST;
    }
    return result;
}

From source file:org.sakaiproject.util.DbResourceBundle.java

/**
 *
 * @param baseName/*from  ww w  . j  av  a  2  s  .c  o  m*/
 * @param locale
 * @param classLoader
 * @return ResourceBundle with values from classpath loading, and overridden by any values
 *         retrieved from the MessageBundleService
 */
static public ResourceBundle addResourceBundle(String baseName, Locale locale, ClassLoader classLoader) {
    DbResourceBundle newBundle = new DbResourceBundle(baseName, locale);
    String context = (String) getThreadLocalManager().get(org.sakaiproject.util.RequestFilter.CURRENT_CONTEXT);
    try {
        if (context != null) {
            Map bundleValues = getMessageBundleService().getBundle(baseName, context, locale);
            Iterator<Entry<String, String>> bundleEntryIter = bundleValues.entrySet().iterator();
            while (bundleEntryIter.hasNext()) {
                Entry<String, String> entry = bundleEntryIter.next();
                String key = entry.getKey();
                String value = entry.getValue();
                newBundle.addProperty(key, value);
            }
        }
        ResourceBundle loadedBundle = null;
        try {
            if (classLoader == null)
                loadedBundle = ResourceBundle.getBundle(baseName, locale);
            else
                loadedBundle = ResourceBundle.getBundle(baseName, locale, classLoader);
        } catch (NullPointerException e) {
        } // ignore

        Enumeration<String> keys = loadedBundle.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            if (newBundle.handleGetObject(key) == null) {
                newBundle.addProperty(key, loadedBundle.getString(key));
            }
        }
    } catch (Exception e) {
        log.error(
                "problem loading bundle: " + baseName + " locale: " + locale.toString() + " " + e.getMessage());
    }

    return newBundle;
}

From source file:org.jahia.izpack.ResourcesConverter.java

/**
 * Performs conversion of the property file into XML language pack.
 * /*from   w ww  .j av a2s. c  o  m*/
 * @param bundleName
 *            the resource bundle name
 * @param locale
 *            locale to be used
 * @param targetFolder
 *            the target folder
 * @throws FileNotFoundException
 */
private static void convert(String bundleName, Locale locale, File targetFolder) throws FileNotFoundException {
    ResourceBundle enBundle = ResourceBundle.getBundle(bundleName, Locale.ENGLISH);
    ResourceBundle bundle = null;
    try {
        bundle = ResourceBundle.getBundle(bundleName, locale);
    } catch (MissingResourceException e) {
        bundle = enBundle;
    }
    PrintWriter out = new PrintWriter(new File(targetFolder,
            StringUtils.substringAfterLast(bundleName, ".") + "." + locale.getISO3Language() + ".xml"));
    Enumeration<String> keyEnum = enBundle.getKeys();
    List<String> keys = new LinkedList<String>();
    while (keyEnum.hasMoreElements()) {
        keys.add(keyEnum.nextElement());
    }
    Collections.sort(keys);
    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    out.println("<langpack>");
    for (String key : keys) {
        String value = null;
        try {
            value = bundle.getString(key);
        } catch (MissingResourceException e) {
            try {
                value = enBundle.getString(key);
            } catch (MissingResourceException e2) {
                value = key;
            }
        }
        out.append("    <str id=\"").append(key).append("\" txt=\"").append(StringEscapeUtils.escapeXml(value))
                .append("\"/>");
        out.println();
    }
    out.println("</langpack>");
    out.flush();
    out.close();
}

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

/**
 * ??,k/v ?map(HashMap).//from w ww  . j av a 2 s. com
 * 
 * @param baseName
 *            ?+??<span style="color:red">(??)</span>,the base name of the resource bundle, a fully qualified class name
 * @param locale
 *            the locale ?
 * @return  baseName key value,null,?,?keyvalue?HashMap
 * @see #getResourceBundle(String, Locale)
 * @see java.util.ResourceBundle#getKeys()
 * @see org.apache.commons.collections.MapUtils#toMap(ResourceBundle)
 */
public static Map<String, String> readAllPropertiesToMap(String baseName, Locale locale) {
    ResourceBundle resourceBundle = getResourceBundle(baseName, locale);
    Enumeration<String> enumeration = resourceBundle.getKeys();
    if (Validator.isNotNullOrEmpty(enumeration)) {
        Map<String, String> propertyMap = new HashMap<String, String>();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            String value = resourceBundle.getString(key);
            propertyMap.put(key, value);
        }
        return propertyMap;
    }
    return null;
}

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

/**
 * ??,k/v ?map(HashMap).//  w w  w.j a v  a  2  s.c  om
 * 
 * @param baseName
 *            ?+??<span style="color:red">(??)</span>,the base name of the resource bundle, a fully qualified class name
 * @param locale
 *            the locale ?
 * @return  baseName key value,null,?,?keyvalue?HashMap
 * @see #getResourceBundle(String, Locale)
 * @see java.util.ResourceBundle#getKeys()
 * @see MapUtils#toMap(ResourceBundle)
 */
public static Map<String, String> readAllPropertiesToMap(String baseName, Locale locale) {
    ResourceBundle resourceBundle = getResourceBundle(baseName, locale);
    Enumeration<String> enumeration = resourceBundle.getKeys();
    if (Validator.isNullOrEmpty(enumeration)) {
        return Collections.emptyMap();
    }

    Map<String, String> propertyMap = new HashMap<String, String>();
    while (enumeration.hasMoreElements()) {
        String key = enumeration.nextElement();
        String value = resourceBundle.getString(key);
        propertyMap.put(key, value);
    }
    return propertyMap;
}

From source file:com.github.jknack.handlebars.springmvc.Handlebars4ViewResolver.java

/**
 * Creates a new i18n source.//from  www  .  j  a v a2 s.  c  o m
 *
 * @param context The application context.
 * @return A new i18n source.
 */
private static I18nSource createI18nSource(final ApplicationContext context) {
    return new I18nSource() {
        @Override
        public String message(final String key, final Locale locale, final Object... args) {
            return context.getMessage(key, args, locale);
        }

        @Override
        public String[] keys(final String basename, final Locale locale) {
            ResourceBundle bundle = ResourceBundle.getBundle(basename, locale);
            Enumeration<String> keys = bundle.getKeys();
            List<String> result = new ArrayList<String>();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                result.add(key);
            }
            return result.toArray(new String[result.size()]);
        }
    };
}

From source file:com.ecyrd.jspwiki.ui.TemplateManager.java

/**
 *  Extract all i18n strings in the javascript domain. (javascript.*)
 *  Returns a javascript snippet which defines the LoacalizedStings array.
 *
 *  @param wiki context/*w w  w.  ja va 2s .c o m*/
 *  @return Javascript snippet which defines the LocaliedStrings array
 *  @author Dirk Frederickx
 *  @since 2.5.108
 */
private static String getJSLocalizedStrings(WikiContext context) {
    StringBuffer sb = new StringBuffer();

    sb.append("var LocalizedStrings = {\n");

    ResourceBundle rb = context.getBundle("templates.default");

    boolean first = true;

    for (Enumeration en = rb.getKeys(); en.hasMoreElements();) {
        String key = (String) en.nextElement();

        if (key.startsWith("javascript")) {
            if (first) {
                first = false;
            } else {
                sb.append(",\n");
            }
            sb.append("\"" + key + "\":\"" + rb.getString(key) + "\"");
        }
    }
    sb.append("\n};\n");

    return (sb.toString());
}

From source file:org.apache.wiki.ui.TemplateManager.java

/**
 *  Extract all i18n strings in the javascript domain. (javascript.*)
 *  Returns a javascript snippet which defines the LoacalizedStings array.
 *
 *  @param wiki context//from w ww  . j a  va2  s .  co m
 *  @return Javascript snippet which defines the LocaliedStrings array
 *  @since 2.5.108
 */
private static String getJSLocalizedStrings(WikiContext context) {
    StringBuffer sb = new StringBuffer();

    sb.append("var LocalizedStrings = {\n");

    ResourceBundle rb = Preferences.getBundle(context, InternationalizationManager.DEF_TEMPLATE);

    boolean first = true;

    for (Enumeration en = rb.getKeys(); en.hasMoreElements();) {
        String key = (String) en.nextElement();

        if (key.startsWith("javascript")) {
            if (first) {
                first = false;
            } else {
                sb.append(",\n");
            }
            sb.append("\"" + key + "\":\"" + rb.getString(key) + "\"");
        }
    }
    sb.append("\n};\n");

    return (sb.toString());
}

From source file:org.vosao.i18n.VosaoResourceBundle.java

@Override
public Enumeration<String> getKeys() {
    List<String> result = new ArrayList<String>();
    for (ResourceBundle bundle : getResourceBundles()) {
        result.addAll(Collections.list(bundle.getKeys()));
    }//from w w w  . j  a v  a 2  s .  c  o  m
    return Collections.enumeration(result);
}

From source file:org.openmrs.module.openhmis.backboneforms.web.controller.BackboneMessageRenderController.java

@RequestMapping(method = RequestMethod.GET)
public ModelAndView render(HttpServletRequest request) {
    Locale locale = RequestContextUtils.getLocale(request);
    ResourceBundle resourceBundle = ResourceBundle.getBundle("messages", locale);
    return new ModelAndView(BackboneWebConstants.MESSAGE_PAGE, "keys", resourceBundle.getKeys());
}