Example usage for java.util ResourceBundle getBundle

List of usage examples for java.util ResourceBundle getBundle

Introduction

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

Prototype

@CallerSensitive
public static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) 

Source Link

Document

Gets a resource bundle using the specified base name, locale, and class loader.

Usage

From source file:Main.java

public static void main(String[] args) {

    ClassLoader cl = ClassLoader.getSystemClassLoader();

    ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US, cl);

    System.out.println(bundle.getString("hello"));

}

From source file:Main.java

public static void main(String[] args) {

    ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

    ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US, rbc);

    System.out.println(bundle.getString("hello"));

}

From source file:PropertyLoader.java

public static Properties loadProperties(String name, ClassLoader loader) throws Exception {
    if (name.startsWith("/"))
        name = name.substring(1);//from   ww w  . j av  a2s . c o m
    if (name.endsWith(SUFFIX))
        name = name.substring(0, name.length() - SUFFIX.length());
    Properties result = new Properties();
    InputStream in = null;
    if (loader == null)
        loader = ClassLoader.getSystemClassLoader();
    if (LOAD_AS_RESOURCE_BUNDLE) {
        name = name.replace('/', '.');
        ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault(), loader);
        for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
            result.put((String) keys.nextElement(), rb.getString((String) keys.nextElement()));
        }
    } else {
        name = name.replace('.', '/');
        if (!name.endsWith(SUFFIX))
            name = name.concat(SUFFIX);
        in = loader.getResourceAsStream(name);
        if (in != null) {
            result = new Properties();
            result.load(in); // can throw IOException
        }
    }
    in.close();
    return result;
}

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

/**
 *  {res}.properties  key /*from w  w  w  .  ja  v  a2 s.com*/
 * 
 * @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:com.hexidec.util.Translatrix.java

public static void init(String bundle, Locale locale) {

    if (langResources != null) {
        return;/*from  w  w  w. ja v  a  2s .c om*/
    }

    try {
        langResources = ResourceBundle.getBundle(bundle, locale, ClassLoader.getSystemClassLoader());
    } catch (MissingResourceException mre) {
        logException("MissingResourceException while loading language file", mre);
    }
}

From source file:com.gettextresourcebundle.GettextResourceBundleControlTest.java

/**
 * test that the controls picks up PO files by locale
 * from the classpath/*from  www.jav a2  s  .  c  om*/
 */
@Test
public void testClasspathReads() {
    ResourceBundle en_US = ResourceBundle.getBundle("locale", Locale.US,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_US for the en_US locale", en_US.getString("locale"), "en_US");

    ResourceBundle en_CA = ResourceBundle.getBundle("locale", Locale.CANADA,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_CA for the en_CA locale", en_CA.getString("locale"), "en_CA");
}

From source file:com.krawler.esp.utils.PropsLoader.java

public static Properties loadProperties(String name, ClassLoader loader) {
    if (name == null)
        throw new IllegalArgumentException("null input: name");

    if (name.startsWith("/"))
        name = name.substring(1);/*from   w  w  w  .ja v  a  2s  .  co  m*/

    if (name.endsWith(SUFFIX))
        name = name.substring(0, name.length() - SUFFIX.length());

    Properties result = null;

    InputStream in = null;
    try {
        if (loader == null)
            loader = ClassLoader.getSystemClassLoader();

        if (LOAD_AS_RESOURCE_BUNDLE) {
            name = name.replace('/', '.');
            // Throws MissingResourceException on lookup failures:
            final ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault(), loader);

            result = new Properties();
            for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
                final String key = (String) keys.nextElement();
                final String value = rb.getString(key);

                result.put(key, value);
            }
        } else {
            name = name.replace('.', '/');

            if (!name.endsWith(SUFFIX))
                name = name.concat(SUFFIX);

            // Returns null on lookup failures:
            in = loader.getResourceAsStream(name);
            if (in != null) {
                result = new Properties();
                result.load(in); // Can throw IOException
            }
        }
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        result = null;
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (Throwable ignore) {
                logger.warn(ignore.getMessage(), ignore);
            }
    }

    if (THROW_ON_LOAD_FAILURE && (result == null)) {
        throw new IllegalArgumentException("could not load [" + name + "]" + " as "
                + (LOAD_AS_RESOURCE_BUNDLE ? "a resource bundle" : "a classloader resource"));
    }

    return result;
}

From source file:com.hypersocket.i18n.I18N.java

public static Set<String> getResourceKeys(Locale locale, String resourceBundle) {
    if (resourceBundle == null) {
        throw new IllegalArgumentException("You must specify a resource bundle");
    }//w  w w . j a  v  a 2 s . c o m

    String bundle = resourceBundle;
    if (!bundle.startsWith("i18n/")) {
        bundle = "i18n/" + resourceBundle;
    }

    Set<String> keys = new HashSet<String>();
    try {
        ResourceBundle rb = ResourceBundle.getBundle(bundle, locale, I18N.class.getClassLoader());
        keys.addAll(rb.keySet());
    } catch (MissingResourceException e) {

    }

    if (hasOverideBundle(locale, resourceBundle)) {
        try {
            ResourceBundle rb = new OverrideResourceBundle(locale, resourceBundle);
            keys.addAll(rb.keySet());
        } catch (IOException e1) {
            log.error("Failed to load override file for bundle " + resourceBundle);
        }
    }

    return keys;
}

From source file:com.clustercontrol.util.Messages.java

/**
 * Returns the resource object with the given key in the resource bundle. If
 * there isn't any value under the given key, the default value is returned.
 * /*w  w w  .ja  v a 2  s.com*/
 * @param key
 *            the resource name
 * @param def
 *            the default value
 * @param locale
 * @return the string
 */
public static String getString(String key, String def, Locale locale) {
    String ret = def;
    ResourceBundle.Control control = ResourceBundle.Control
            .getNoFallbackControl(ResourceBundle.Control.FORMAT_DEFAULT);
    if (clientFlag) {
        ResourceBundle bundle = null;
        try {
            if (locale != null) {
                bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_CLIENT, locale, control);
            } else {
                bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_CLIENT, control);
            }
        } catch (MissingResourceException e) {
            m_log.info(RESOURCE_BUNDLE_CLIENT + " missing"); // Client???1??
            clientFlag = false;
        }
        try {
            if (clientFlag) {
                ret = bundle.getString(key);
            }
        } catch (MissingResourceException e) {
        }
    }
    if (commonFlag) {
        ResourceBundle bundle = null;
        try {
            if (locale != null) {
                bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_COMMON, locale, control);
            } else {
                bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_COMMON, control);
            }
        } catch (MissingResourceException e) {
            m_log.warn(RESOURCE_BUNDLE_COMMON + " missing"); // ???????
            commonFlag = false;
        }
        try {
            if (commonFlag) {
                ret = bundle.getString(key);
            }
        } catch (MissingResourceException e) {
        }
    }

    return ret;
}

From source file:com.gettextresourcebundle.GettextResourceBundleControlTest.java

/**
 * test that the controls picks up PO files by locale
 * from the filesystem//  ww  w.j  av a  2  s . c  o m
 */
@Test
public void testFileReads() {
    ResourceBundle en_US = ResourceBundle.getBundle("./src/test/resources/locale", Locale.US,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_US for the en_US locale", en_US.getString("locale"), "en_US");

    ResourceBundle en_CA = ResourceBundle.getBundle("./src/test/resources/locale", Locale.CANADA,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_CA for the en_CA locale", en_CA.getString("locale"), "en_CA");
}