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:es.urjc.mctwp.bbeans.AbstractBean.java

/**
 * Get a message from the bundle using the key
 *///from  ww  w  .j av a  2 s .c o  m
protected String getMessage(String key) {
    String text = null;

    Locale locale = fc.getViewRoot().getLocale();
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    String bundleName = fc.getApplication().getMessageBundle();

    ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale, cl);

    try {
        text = bundle.getString(key);
    } catch (MissingResourceException e) {
        text = "?? key " + key + " not found ??";
    }

    return text;
}

From source file:Loader.java

public static ResourceBundle getResourceBundle(Class loadClass, String name, boolean checkParents,
        Locale locale) throws MissingResourceException {
    MissingResourceException ex = null;
    ResourceBundle bundle = null;
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    while (bundle == null && loader != null) {
        try {/*from ww w  .j ava 2 s .c  o m*/
            bundle = ResourceBundle.getBundle(name, locale, loader);
        } catch (MissingResourceException e) {
            if (ex == null)
                ex = e;
        }
        loader = (bundle == null && checkParents) ? loader.getParent() : null;
    }

    loader = loadClass == null ? null : loadClass.getClassLoader();
    while (bundle == null && loader != null) {
        try {
            bundle = ResourceBundle.getBundle(name, locale, loader);
        } catch (MissingResourceException e) {
            if (ex == null)
                ex = e;
        }
        loader = (bundle == null && checkParents) ? loader.getParent() : null;
    }

    if (bundle == null) {
        try {
            bundle = ResourceBundle.getBundle(name, locale);
        } catch (MissingResourceException e) {
            if (ex == null)
                ex = e;
        }
    }

    if (bundle != null)
        return bundle;
    throw ex;
}

From source file:com.projity.strings.Messages.java

public static String getMetaString(String key) {
    if (metaBundle == null) {
        lock.lock(); //use lock to avoid useless synchronized when it's already initialized
        try {//  w w w .  j  a va  2  s .co  m
            if (metaBundle == null) { //if it hasn't been initialized by an other thread
                metaBundle = ResourceBundle.getBundle(META_BUNDLE_NAME, Locale.getDefault(),
                        ClassLoaderUtils.getLocalClassLoader()/*Messages.class.getClassLoader()*/);
            }
        } finally {
            lock.unlock();
        }
    }
    return metaBundle.getString(key);
}

From source file:com.icesoft.faces.utils.MessageUtils.java

protected static String getResource(String bundleName, Locale locale, String messageId) {
    ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale, getClassLoader(bundleName));
    String ret = null;/* w  ww. j a v a 2  s. com*/
    try {
        ret = bundle.getString(messageId);
    } catch (Exception e) {
    }
    return ret;
}

From source file:de.awtools.lang.TranslatorFactory.java

/**
 * Liefert einen <code>Translator</code>.
 *
 * @param basename Siehe in der Beschreibung zu <code>ResourceBundle</code>
 *  in der Java API.//  w  w w. j a va 2 s  . co m
 * @param locale Die gewnschte Sprache.
 * @param loader Der zu verwendende <code>ClassLoader</code>.
 * @return Liefert einen <code>Translator</code>.
 */
public Translator getTranslator(final String basename, final Locale locale, final ClassLoader loader) {

    CacheKey ck = new CacheKey(basename, locale, loader);
    Translator translator = transes.get(ck);
    if (translator == null) {
        translator = new Translator(ResourceBundle.getBundle(basename, locale, loader));
        transes.put(ck, translator);
    }

    return translator;
}

From source file:edu.cornell.mannlib.vitro.webapp.i18n.I18n.java

/**
 * Get an I18nBundle by this name. The request provides the preferred
 * Locale, the application directory, the theme directory and the
 * development mode flag.//from w  ww  . j  a  v a 2  s.  c o  m
 * 
 * If the request indicates that the system is in development mode, then the
 * cache is cleared on each request.
 * 
 * If the theme directory has changed, the cache is cleared.
 * 
 * Declared 'protected' so it can be overridden in unit tests.
 */
protected I18nBundle getBundle(String bundleName, HttpServletRequest req) {
    log.debug("Getting bundle '" + bundleName + "'");

    I18nLogger i18nLogger = new I18nLogger();
    try {
        checkDevelopmentMode(req);
        checkForChangeInThemeDirectory(req);

        String dir = themeDirectory.get();
        ServletContext ctx = req.getSession().getServletContext();

        ResourceBundle.Control control = new ThemeBasedControl(ctx, dir);
        ResourceBundle rb = ResourceBundle.getBundle(bundleName, req.getLocale(), control);
        return new I18nBundle(bundleName, rb, i18nLogger);
    } catch (MissingResourceException e) {
        log.warn("Didn't find text bundle '" + bundleName + "'");
        return I18nBundle.emptyBundle(bundleName, i18nLogger);
    } catch (Exception e) {
        log.error("Failed to create text bundle '" + bundleName + "'", e);
        return I18nBundle.emptyBundle(bundleName, i18nLogger);
    }
}

From source file:it.imtech.configuration.ChooseServer.java

/**
 * Connection serrver test, acquiring certificate if exists.
 * @param uri - Selected server URI//from ww  w.  j a v a 2  s .  c  o m
 * @return 
 */
public static boolean testServerConnection(String uri) {
    Server s = (Server) choose_server.getSelectedItem();

    boolean result = true;
    String outputFile = Globals.USER_DIR + "certs" + Utility.getSep() + "jssecacerts.jks";

    //Aggiungo Keystore Temporaneo
    if (new File(outputFile).isFile()) {
        System.setProperty("javax.net.ssl.keyStore", outputFile);
        System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
        System.setProperty("javax.net.ssl.trustStore", outputFile);
        System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
    }

    try {
        URL url = new URL("https://" + uri);
        URLConnection con = url.openConnection();
        Reader reader = new InputStreamReader(con.getInputStream());
    } catch (SSLHandshakeException ex) {
        ResourceBundle bundle = ResourceBundle.getBundle(Globals.RESOURCES, Globals.CURRENT_LOCALE,
                Globals.loader);
        Object[] options = { Utility.getBundleString("voc1", bundle), Utility.getBundleString("voc2", bundle) };

        int n = JOptionPane.showOptionDialog(null, Utility.getBundleString("phcertadd", bundle),
                Utility.getBundleString("phcertadd", bundle), JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

        if (n == JOptionPane.YES_OPTION) {
            String[] run = new String[1];
            run[0] = uri + ":443";
            AddToStoreKey.createAndShowGUI(run);
            result = false;
        }
    } catch (Exception ex) {
    }

    return result;
}

From source file:de.unentscheidbar.validation.swing.DefaultMessageTextGetter.java

private String getFormatString(ValidationMessage.Id messageId, Locale locale) throws MissingResourceException {

    ResourceBundle bundle = ResourceBundle.getBundle(getBundleName(messageId), locale,
            messageId.getClass().getClassLoader());
    return String.valueOf(bundle.getObject(messageId.name()));
}

From source file:com.projity.strings.Messages.java

private static String getStringFromBundles(String key) {
    if (key == null)
        return null;
    LinkedList<ResourceBundle> buns = new LinkedList<ResourceBundle>();
    LinkedList<String> foundBundles = new LinkedList<String>();
    ;// w  w w  .j  av  a2  s.  c o  m
    if (bundles == null) {
        lock.lock(); //use lock to avoid useless synchronized when it's already initialized
        try {
            if (bundles == null) { //if it hasn't been initialized by an other thread
                String bundleNames[] = getMetaString("ResourceBundles").split(";");
                String directoryBundleNames[] = getMetaString("DirectoryResourceBundles").split(";");
                if (directoryClassLoader.isValid()) {
                    //foundBundles=new ArrayList<String>(bundleNames.length+directoryBundleNames.length);

                    for (int i = 0; i < directoryBundleNames.length; i++) {
                        try {
                            ResourceBundle bundle = ResourceBundle.getBundle(directoryBundleNames[i],
                                    Locale.getDefault(), directoryClassLoader);
                            buns.add(bundle);
                            foundBundles.add("com.projity.strings." + directoryBundleNames[i]);
                        } catch (Exception e) {
                        }
                    }
                } else
                    buns = new LinkedList<ResourceBundle>();
                for (int i = bundleNames.length - 1; i >= 0; i--) { // reverse order since the later ones should be searched first
                    String bname = bundleNames[i];

                    //find right position to insert in bundles
                    int j = 0;
                    int pos = 0;
                    for (String b : foundBundles) {
                        if (bname.equals(b))
                            break;
                        pos++;
                    }
                    buns.add(pos, ResourceBundle.getBundle(bname, Locale.getDefault(),
                            ClassLoaderUtils.getLocalClassLoader()/*Messages.class.getClassLoader()*/));
                    foundBundles.add(pos, bname);
                }
            }
        } finally {
            bundles = buns;
            lock.unlock();
        }
    }
    for (ResourceBundle bundle : bundles) {
        try {
            return bundle.getString(key);
        } catch (MissingResourceException e) {
        }
    }
    return null;
}