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.apache.sling.servlethelpers.MockSlingHttpServletRequestTest.java

@Test
public void testDefaultResourceBundle() {
    ResourceBundle bundle = request.getResourceBundle(Locale.US);
    assertNotNull(bundle);/* w w w .j av a2s. c om*/
    assertFalse(bundle.getKeys().hasMoreElements());
}

From source file:com.sunrun.crportal.util.CRPortalUtil.java

public static Properties resourceBundleToProperties(ResourceBundle rb) {
    Properties properties = new Properties();

    Enumeration<String> keys = rb.getKeys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        properties.put(key, rb.getString(key));
    }/*from  w ww  .  ja v a2  s.  c  o  m*/

    return properties;
}

From source file:org.sakaiproject.component.section.SectionAwarenessHibernateImpl.java

/**
 * @inheritDoc//from  ww  w.  ja  v  a  2  s  . c  om
 */
public List getSectionCategories(String siteContext) {
    ResourceBundle bundle = ResourceBundle.getBundle(SectionManagerHibernateImpl.CATEGORY_BUNDLE, Locale.US);

    Enumeration keys = bundle.getKeys();
    List categoryIds = new ArrayList();
    while (keys.hasMoreElements()) {
        categoryIds.add(keys.nextElement());
    }
    Collections.sort(categoryIds);
    return categoryIds;
}

From source file:org.webguitoolkit.ui.controls.util.TextService.java

/**
 * this lists all translation where the key startWith 'prefix'.
 * /*from www. j a v  a 2s .  co m*/
 * @param prefix
 * @return List od String[2], each holding (key, translation)
 */
public List prefixKey(String prefix) {
    List list = new LinkedList();
    String key;
    // the resource bundle holding the translation for current locale
    final ResourceBundle bundle = bundle(getLocale());
    // go through all key and find those with prefix
    for (Enumeration en = bundle.getKeys(); en.hasMoreElements();) {
        key = (String) en.nextElement();
        if ((key != null) && key.startsWith(prefix)) {
            // prefix found, add key and translation to list
            // so it can be loaded in select
            String[] entry = new String[] { key.substring(prefix.length()), getString(key) };
            list.add(entry);
        }
    }
    if (list.isEmpty()) {
        UnTranslatedTextCollector.addKey(prefix, "All keys for this prefix are missing.");
    }
    return list;
}

From source file:net.jawr.web.resource.bundle.locale.message.GrailsMessageBundleScriptCreator.java

public Reader createScript(Charset charset) {

    // Determine wether this is run-app or run-war style of runtime. 
    boolean warDeployed = ((Boolean) this.servletContext.getAttribute(JawrConstant.GRAILS_WAR_DEPLOYED))
            .booleanValue();//from  w  ww.j  a  v  a 2  s . c  o m

    // Spring message bundle object, the same used by grails. 
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setFallbackToSystemLocale(false);
    Set<String> allPropertyNames = null;

    // Read the properties files to find out the available message keys. It is done differently 
    // for run-app or run-war style of runtimes. 
    if (warDeployed) {
        allPropertyNames = getPropertyNamesFromWar();
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("found a total of " + allPropertyNames.size() + " distinct message keys.");
        messageSource.setResourceLoader(new ServletContextResourceLoader(this.servletContext));
        messageSource.setBasename(PROPERTIES_DIR + configParam.substring(configParam.lastIndexOf('.') + 1));
    } else {
        ResourceBundle bundle;
        if (null != locale)
            bundle = ResourceBundle.getBundle(configParam, locale);
        else
            bundle = ResourceBundle.getBundle(configParam);
        allPropertyNames = new HashSet<String>();
        Enumeration<String> keys = bundle.getKeys();
        while (keys.hasMoreElements())
            allPropertyNames.add(keys.nextElement());
        String basename = "file:./" + configParam.replaceAll("\\.", "/");
        messageSource.setBasename(basename);
    }

    // Pass all messages to a properties file. 
    Properties props = new Properties();
    for (Iterator<String> it = allPropertyNames.iterator(); it.hasNext();) {
        String key = it.next();
        if (matchesFilter(key)) {
            try {
                // Use the property encoding of the file
                String msg = new String(
                        messageSource.getMessage(key, new Object[0], locale).getBytes(CHARSET_ISO_8859_1),
                        charset.displayName());
                props.put(key, msg);
            } catch (NoSuchMessageException e) {
                // This is expected, so it's OK to have an empty catch block. 
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Message key [" + key + "] not found.");
            } catch (UnsupportedEncodingException e) {
                LOGGER.warn("Unable to convert value of message bundle associated to key '" + key
                        + "' because the charset is unknown");
            }
        }
    }

    return doCreateScript(props);
}

From source file:com.cubusmail.gwtui.server.services.UserAccountService.java

public String[][] retrieveTimezones() {

    NumberFormat format = new DecimalFormat("00");
    Locale locale = SessionManager.get().getLocale();
    ResourceBundle bundle = ConvertUtil.getTimezonesBundle(locale);
    Enumeration<String> ids = bundle.getKeys();
    List<TimeZone> timeZoneList = toSortedZimeZoneList(ids);
    String[][] result = new String[timeZoneList.size()][2];
    int index = 0;
    for (TimeZone timeZone : timeZoneList) {
        result[index][0] = timeZone.getID();
        String hours = format.format(timeZone.getRawOffset() / 3600000);
        if (timeZone.getRawOffset() >= 0) {
            result[index][1] = "(+" + hours + ":00) " + bundle.getString(timeZone.getID());
        } else {/*from w  ww. j av a2 s .  c  om*/
            result[index][1] = "(" + hours + ":00) " + bundle.getString(timeZone.getID());
        }
        index++;
    }

    return result;
}

From source file:es.mityc.firmaJava.configuracion.Configuracion.java

private void cargarConfiguracionPorDefecto() {

    log.debug(CARGA_CONFIGURACION_DEFECTO);
    Enumeration<String> enuClaves = null;
    ResourceBundle propiedadesPorDefecto = ResourceBundle.getBundle(FICHERO_RESOURCE);
    configuracion = new HashMap<String, String>();
    enuClaves = propiedadesPorDefecto.getKeys();
    boolean hasNext = enuClaves.hasMoreElements();
    while (hasNext) {
        String clave = enuClaves.nextElement();
        hasNext = enuClaves.hasMoreElements();
        configuracion.put(clave, propiedadesPorDefecto.getString(clave));
    }/*w ww. j  a  v  a 2 s.c  o m*/

}

From source file:org.eclipse.virgo.ide.ui.editors.SpringBundleSourcePage.java

private boolean isSpringHeader(String key) {
    ResourceBundle bundle = ResourceBundle.getBundle("org.eclipse.virgo.ide.ui.editors.text.headers");
    List<String> headers = Collections.list(bundle.getKeys());
    return headers.contains(key);
}

From source file:org.apache.click.util.MessagesMap.java

/**
 * Load the values of the given resourceBundleName into the map.
 *
 * @param resourceBundleName the resource bundle name
 * @param map the map to load resource values into
 *//*from   w ww. ja v  a 2 s.  c  o m*/
protected void loadResourceValuesIntoMap(String resourceBundleName, Map<String, String> map) {
    if (resourceBundleName == null) {
        return;
    }

    String resourceKey = resourceBundleName + locale.toString();

    if (!NOT_FOUND_CACHE.contains(resourceKey)) {
        try {
            ResourceBundle resources = createResourceBundle(resourceBundleName, locale);

            Enumeration<String> e = resources.getKeys();
            while (e.hasMoreElements()) {
                String name = e.nextElement();
                String value = resources.getString(name);
                map.put(name, value);
            }

        } catch (MissingResourceException mre) {
            NOT_FOUND_CACHE.add(resourceKey);
        }
    }
}

From source file:org.pentaho.osgi.i18n.webservice.ResourceBundleMessageBodyWriter.java

@Override
public void writeTo(ResourceBundle resourceBundle, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
        JSONObject resourceBundleJsonObject = new JSONObject();
        for (String key : Collections.list(resourceBundle.getKeys())) {
            resourceBundleJsonObject.put(key, resourceBundle.getString(key));
        }//  w w  w  .j  a  v  a2  s. c om
        OutputStreamWriter outputStreamWriter = null;
        try {
            outputStreamWriter = new OutputStreamWriter(entityStream);
            resourceBundleJsonObject.writeJSONString(outputStreamWriter);
        } finally {
            if (outputStreamWriter != null) {
                outputStreamWriter.flush();
            }
        }
    } else if (MediaType.APPLICATION_XML_TYPE.equals(mediaType)) {
        try {
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Node propertiesNode = document.createElement("properties");
            document.appendChild(propertiesNode);
            for (String key : Collections.list(resourceBundle.getKeys())) {
                Node propertyNode = document.createElement("property");
                propertiesNode.appendChild(propertyNode);

                Node keyNode = document.createElement("key");
                keyNode.setTextContent(key);
                propertyNode.appendChild(keyNode);

                Node valueNode = document.createElement("value");
                valueNode.setTextContent(resourceBundle.getString(key));
                propertyNode.appendChild(valueNode);
            }
            Result output = new StreamResult(entityStream);
            Source input = new DOMSource(document);
            try {
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
                transformer.transform(input, output);
            } catch (TransformerException e) {
                throw new IOException(e);
            }
        } catch (ParserConfigurationException e) {
            throw new WebApplicationException(e);
        }
    }
}