Example usage for java.util ResourceBundle getLocale

List of usage examples for java.util ResourceBundle getLocale

Introduction

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

Prototype

public Locale getLocale() 

Source Link

Document

Returns the locale of this resource bundle.

Usage

From source file:Main.java

public static void main(String[] args) {

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

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

    System.out.println(bundle.getLocale().toString());

}

From source file:com.examples.with.different.packagename.concolic.MathRuntimeException.java

/**
 * Translate a string to a given locale.
 * @param s string to translate/*from  w w  w  .jav  a 2 s.  co  m*/
 * @param locale locale into which to translate the string
 * @return translated string or original string
 * for unsupported locales or unknown strings
 */
private static String translate(final String s, final Locale locale) {
    try {
        ResourceBundle bundle = ResourceBundle.getBundle("org.apache.commons.math.MessagesResources", locale);
        if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
            // the value of the resource is the translated string
            return bundle.getString(s);
        }

    } catch (MissingResourceException mre) {
        // do nothing here
    }

    // the locale is not supported or the resource is unknown
    // don't translate and fall back to using the string as is
    return s;

}

From source file:com.heliosphere.demeter.base.resource.bundle.ResourceBundleManager.java

/**
 * Refresh all the resource bundles already handled by the
 * {@link ResourceBundleManager} for the given {@link Locale}.
 * <p>//w w w  . ja va  2 s .c om
 * @param locale {@link Locale} corresponding to the new language to use.
 */
@SuppressWarnings("nls")
private static final void refresh(final Locale locale) {
    if (NAMES.isEmpty()) {
        ResourceBundleManager.locale = locale;
    }

    for (Class<? extends IBundle> bundleClass : NAMES.keySet()) {
        final ResourceBundle bundle = ResourceBundle.getBundle(NAMES.get(bundleClass), locale);
        if (bundle != null && bundle.getLocale().getLanguage() != locale.getLanguage()) {
            log.error("Bundle cannot be found [name=" + NAMES.get(bundleClass) + ", locale=" + locale.toString()
                    + "]. Using default one [name=" + NAMES.get(bundleClass) + ", locale="
                    + getLocale().toString() + "]");
            ResourceBundleManager.locale = english;
        } else {
            ResourceBundleManager.locale = locale;
        }

        BUNDLES.clear();
        register(bundleClass, bundle);
    }
}

From source file:com.heliosphere.athena.base.resource.bundle.ResourceBundleManager.java

/**
 * Refresh all the resource bundles already handled by the
 * {@link ResourceBundleManager} for the given {@link Locale}.
 * <p>//from ww  w . j  av  a  2s .c o  m
 * @param locale {@link Locale} corresponding to the new language to use.
 */
@SuppressWarnings("nls")
private static final void refresh(final Locale locale) {
    if (NAMES.isEmpty()) {
        ResourceBundleManager.locale = locale;
    }

    for (Class<? extends IBundle> bundleClass : NAMES.keySet()) {
        final ResourceBundle bundle = ResourceBundle.getBundle(NAMES.get(bundleClass), locale);
        if (bundle != null && !bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
            log.error("Bundle cannot be found [name=" + NAMES.get(bundleClass) + ", locale=" + locale.toString()
                    + "]. Using default one [name=" + NAMES.get(bundleClass) + ", locale="
                    + getLocale().toString() + "]");
            ResourceBundleManager.locale = english;
        } else {
            ResourceBundleManager.locale = locale;
        }

        BUNDLES.clear();
        register(bundleClass, bundle);
    }
}

From source file:com.heliosphere.athena.base.resource.bundle.ResourceBundleManager.java

/**
 * Registers a resource bundle using a resource bundle enumeration class
 * (implementing the {@link IBundle} interface).
 * <p>/*from   w ww  .  j a va  2  s  .  c  o  m*/
 * @param bundleEnumClass Resource bundle enumeration class.
 */
@Synchronized
public static final void register(final Class<? extends IBundle> bundleEnumClass) {
    IBundle value = bundleEnumClass.getEnumConstants()[0]; // BUNDLE_FILENAME must be at index 0!
    if (value.toString().equals(BUNDLE_FILENAME)) {
        String filename = value.getKey();

        final ResourceBundle bundle = ResourceBundle.getBundle(filename, locale);

        // Ensure the loaded bundle is for the required language.
        if (bundle.getLocale().getISO3Language().equals(locale.getISO3Language())) {
            register(bundleEnumClass, bundle);
        } else {
            throw new ResourceBundleException(BundleAthenaBase.ResourceBundleError, filename, locale);
        }
    }
}

From source file:com.heliosphere.demeter.base.resource.bundle.ResourceBundleManager.java

/**
 * Registers a resource bundle using a resource bundle enumeration class
 * (implementing the {@link IBundle} interface).
 * <p>//  ww  w  .  j av  a  2  s  .c o  m
 * @param bundleEnumClass Resource bundle enumeration class.
 */
@Synchronized
public static final void register(final Class<? extends IBundle> bundleEnumClass) {
    IBundle value = bundleEnumClass.getEnumConstants()[0]; // BUNDLE_FILENAME must be at index 0!
    if (value.toString().equals(BUNDLE_FILENAME)) {
        String filename = value.getKey();

        final ResourceBundle bundle = ResourceBundle.getBundle(filename, locale);

        // Ensure the loaded bundle is for the required language.
        if (bundle.getLocale().getISO3Language().equals(locale.getISO3Language())) {
            register(bundleEnumClass, bundle);
        } else {
            throw new ResourceBundleException(BundleDemeterBase.ResourceBundleError, filename, locale);
        }
    }
}

From source file:com.heliosphere.athena.base.resource.bundle.ResourceBundleManager.java

/**
 * Registers the resource bundles defined in a given properties file.
 * <p>//w w w. java  2s  .c  o  m
 * @param properties {@link PropertiesConfiguration} file containing the
 * resource bundles to register.
 */
@SuppressWarnings({ "nls", "unchecked" })
private static final void register(final PropertiesConfiguration properties) {
    String value;
    String name;

    if (properties == null) {
        throw new InvalidArgumentException(BundleAthenaBase.ResourceBundleInvalidConfiguration);
    }

    final Iterator<String> iter = properties.getKeys();
    while (iter != null && iter.hasNext()) {
        value = iter.next();
        name = properties.getString(value);

        final ResourceBundle bundle = ResourceBundle.getBundle(name, locale);

        // Ensure the loaded bundle is for the required language.
        if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
            try {
                register((Class<? extends IBundle>) Class.forName(value), bundle);
            } catch (ClassNotFoundException e) {
                String message = MessageFormat.format(
                        "Cannot register properties [file={0}, class={1}, reason={2}, resolution={3}]", name,
                        value, "The given class cannot be found",
                        "Ensure the given class exist as an enumeration class and implements the IBundle interface");

                log.error(message, e);

                // Cannot get resource bundle, so message must be in raw format.
                throw new ResourceBundleException(MessageFormat.format(
                        "Cannot register properties [file={0}, class={1}, reason={2}, resolution={3}]", name,
                        value, "The given class cannot be found",
                        "Ensure the given class exist as an enumeration class and implements the IBundle interface"));
            }
        } else {
            // Cannot get resource bundle, so message must be in raw format.
            throw new ResourceBundleException(
                    MessageFormat.format("Bundle cannot be found [name={0}, locale={1}]", name, locale));
        }
    }
}

From source file:com.heliosphere.demeter.base.resource.bundle.ResourceBundleManager.java

/**
 * Registers the resource bundles defined in a given properties file.
 * <p>/*from   w ww.  java 2  s. c  o m*/
 * @param properties {@link PropertiesConfiguration} file containing the
 * resource bundles to register.
 */
@SuppressWarnings({ "nls", "unchecked" })
private static final void register(final PropertiesConfiguration properties) {
    String value;
    String name;

    if (properties == null) {
        throw new InvalidArgumentException(BundleDemeterBase.ResourceBundleInvalidConfiguration);
    }

    final Iterator<String> iter = properties.getKeys();
    while (iter != null && iter.hasNext()) {
        value = iter.next();
        name = properties.getString(value);

        final ResourceBundle bundle = ResourceBundle.getBundle(name, locale);

        // Ensure the loaded bundle is for the required language.
        if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
            try {
                register((Class<? extends IBundle>) Class.forName(value), bundle);
            } catch (ClassNotFoundException e) {
                String message = MessageFormat.format(
                        "Cannot register properties [file={0}, class={1}, reason={2}, resolution={3}]", name,
                        value, "The given class cannot be found",
                        "Ensure the given class exist as an enumeration class and implements the IBundle interface");

                log.error(message, e);

                // Cannot get resource bundle, so message must be in raw format.
                throw new ResourceBundleException(MessageFormat.format(
                        "Cannot register properties [file={0}, class={1}, reason={2}, resolution={3}]", name,
                        value, "The given class cannot be found",
                        "Ensure the given class exist as an enumeration class and implements the IBundle interface"));
            }
        } else {
            // Cannot get resource bundle, so message must be in raw format.
            throw new ResourceBundleException(
                    MessageFormat.format("Bundle cannot be found [name={0}, locale={1}]", name, locale));
        }
    }
}

From source file:org.itracker.web.util.LoginUtilities.java

/**
 * Get a locale from request/*from w ww.j  a  v  a 2 s  .c om*/
 * <p/>
 * <p>
 * TODO the order of retrieving locale from request should be:
 * <ol>
 * <li>request-attribute Constants.LOCALE_KEY</li>
 * <li>request-param 'loc'</li>
 * <li>session attribute <code>Constants.LOCALE_KEY</code></li>
 * <li>cookie 'loc'</li>
 * <li>request.getLocale()/request.getLocales()</li>
 * <li>ITrackerResources.DEFAULT_LOCALE</li>
 * </ol>
 * </p>
 */
@SuppressWarnings("unchecked")
public static Locale getCurrentLocale(HttpServletRequest request) {
    Locale requestLocale = null;
    HttpSession session = request.getSession(true);
    try {

        requestLocale = (Locale) request.getAttribute(Constants.LOCALE_KEY);

        if (logger.isDebugEnabled()) {
            logger.debug("getCurrentLocale: request-attribute was {}", requestLocale);
        }

        if (null == requestLocale) {
            // get locale from request param
            String loc = request.getParameter("loc");
            if (null != loc && loc.trim().length() > 1) {
                requestLocale = ITrackerResources.getLocale(loc);
            }

            logger.debug("getCurrentLocale: request-parameter was {}", loc);

        }

        if (null == requestLocale) {
            // get it from the session
            requestLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
            //            if (logger.isDebugEnabled()) {
            //               logger.debug("getCurrentLocale: session-attribute was "
            //                     + requestLocale);
            //            }
        }

        if (null == requestLocale) {
            ResourceBundle bundle = ITrackerResources.getBundle(request.getLocale());
            if (logger.isDebugEnabled()) {
                logger.debug("getCurrentLocale: trying request header locale " + request.getLocale());
            }
            if (bundle.getLocale().getLanguage().equals(request.getLocale().getLanguage())) {
                requestLocale = request.getLocale();
                if (logger.isDebugEnabled()) {
                    logger.debug("getCurrentLocale: request-locale was " + requestLocale);
                }
            }
        }

        // is there no way to detect supported locales of current
        // installation?

        if (null == requestLocale) {
            Enumeration<Locale> locales = (Enumeration<Locale>) request.getLocales();
            ResourceBundle bundle;
            Locale locale;
            while (locales.hasMoreElements()) {
                locale = locales.nextElement();
                bundle = ITrackerResources.getBundle(locale);

                logger.debug("getCurrentLocale: request-locales processing {}, bundle: {}", locale, bundle);

                if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
                    requestLocale = locale;

                    logger.debug("getCurrentLocale: request-locales locale was {}", requestLocale);

                }
            }
        }

    } finally {
        if (null == requestLocale) {
            // fall back to default locale
            requestLocale = ITrackerResources.getLocale();

            logger.debug("getCurrentLocale: fallback default locale was {}", requestLocale);

        }
        session.setAttribute(Constants.LOCALE_KEY, requestLocale);
        request.setAttribute(Constants.LOCALE_KEY, requestLocale);
        request.setAttribute("currLocale", requestLocale);

        logger.debug("getCurrentLocale: request and session was setup with {}", requestLocale);

    }

    return requestLocale;
}

From source file:com.mfalaize.ant.LocalizeTask.java

/**
 * Get the list of available/*from  w w  w. j a va  2  s  .c  o m*/
 * <code>ResourceBundle</code> in the project.
 *
 * @return The list of <code>ResourceBundle</code> available in the project
 * to build.
 * @throws BuildException when the <code>ResourceBundle</code> cannot be
 * find.
 */
private List<ResourceBundle> getAvailableResourceBundles() throws BuildException {
    List<ResourceBundle> list = new ArrayList<ResourceBundle>();

    for (Locale locale : Locale.getAvailableLocales()) {
        try {
            ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceBundleBaseName, locale);

            if (locale.equals(resourceBundle.getLocale())) {
                list.add(resourceBundle);
            } else {
                log(String.format("No resource bundle exists for the locale %s. Continue...",
                        locale.getLanguage()), Project.MSG_VERBOSE);
            }
        } catch (Throwable ex) {
            throw new BuildException(ex);
        }
    }

    return list;
}