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:org.apache.openejb.math.MathRuntimeException.java

/**
 * Translate a string to a given locale.
 *
 * @param s      string to translate/*  w w w  .j  av  a  2s. c  om*/
 * @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 {
        final 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 (final 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:org.geomajas.plugin.printing.component.ComponentUtilTest.java

@Test
public void testGetResourceBundleEn() throws Exception {
    String locale = "en";
    ResourceBundle resourceBundle = ComponentUtil.getCurrentResourceBundle(locale);
    Assert.assertEquals(locale.toLowerCase(), resourceBundle.getLocale().toString().toLowerCase());
}

From source file:org.geomajas.plugin.printing.component.ComponentUtilTest.java

@Test
public void testGetResourceBundleNl() throws Exception {
    String locale = "nl";
    ResourceBundle resourceBundle = ComponentUtil.getCurrentResourceBundle(locale);
    Assert.assertEquals(locale.toLowerCase(), resourceBundle.getLocale().toString().toLowerCase());
}

From source file:org.geomajas.plugin.printing.component.ComponentUtilTest.java

@Test
public void testGetResourceBundleFallbackOnLanguage() throws Exception {
    String locale = "en_GB";
    ResourceBundle resourceBundle = ComponentUtil.getCurrentResourceBundle(locale);
    Assert.assertEquals("en", resourceBundle.getLocale().toString().toLowerCase());
}

From source file:org.geomajas.plugin.printing.component.ComponentUtilTest.java

@Test
public void testGetResourceBundleEnUs() throws Exception {
    String locale = "en_US";
    ResourceBundle resourceBundle = ComponentUtil.getCurrentResourceBundle(locale);
    Assert.assertEquals(locale.toLowerCase(), resourceBundle.getLocale().toString().toLowerCase());
}

From source file:org.geomajas.plugin.printing.component.ComponentUtilTest.java

@Test
public void testGetResourceBundleUnknown() throws Exception {
    String locale = "xyz";
    String[] defaultLocaleArray = new String[] { "en", "nl" };
    for (String defaultLocale : defaultLocaleArray) {
        Locale.setDefault(LocaleUtils.toLocale(defaultLocale));
        ResourceBundle resourceBundle = ComponentUtil.getCurrentResourceBundle(locale);
        Assert.assertEquals(defaultLocale.toLowerCase(), resourceBundle.getLocale().toString().toLowerCase());
    }//from   www.j a v a  2 s  .  c  o m
}

From source file:org.itracker.model.IssueField.java

/**
 * Gets the custom field value as a String.
 *
 * @param bundle a resource bundle to use for any string formatting
 * @return the current value of this field
 * @deprecated this can not be in the entity, replace by Utility or service.
 *//*from   w  w w  .  jav  a2  s .c o  m*/
public String getValue(ResourceBundle bundle) {

    // skip this code, it's not approved
    Locale locale = bundle.getLocale();

    if (log.isDebugEnabled()) {
        log.debug("getValue: called with bundle: " + bundle + ", locale: " + locale);
    }
    switch (customField.getFieldType()) {

    case INTEGER:
        if (log.isDebugEnabled()) {
            log.debug("getValue: type was INTEGER, value: " + this.intValue);
        }
        return String.valueOf(this.intValue);

    case DATE:
        if (log.isDebugEnabled()) {
            log.debug("getValue: type was DATE, value: " + this.dateValue);
        }
        if (!customField.isRequired() && this.dateValue == null) {

            if (log.isDebugEnabled()) {
                log.debug("getValue: value was null and not required");
            }
            return null;
        }
        if (this.dateValue == null) {
            this.dateValue = new Date();
        }
        return formatDate(bundle);
    default:
        return this.stringValue;
    }

}

From source file:org.itracker.model.IssueField.java

private String formatDate(ResourceBundle bundle) {
    assert (dateValue != null) : "dateValue failed";
    try {//from  w  w  w . ja va2s . co m

        SimpleDateFormat sdf = new SimpleDateFormat(
                bundle.getString("itracker.dateformat." + customField.getDateFormat()), bundle.getLocale());

        if (log.isDebugEnabled()) {
            log.debug("getValue: dateFormat from itracker configuration " + sdf.toPattern());
        }

        // sdf = new SimpleDateFormat(dateFormat, locale);
        String formattedDate = sdf.format(this.dateValue);
        if (log.isDebugEnabled()) {
            log.debug("getValue: formated date " + this.dateValue + " to " + formattedDate);
        }
        return formattedDate;
    } catch (NullPointerException ne) {
        log.debug("getValue: ", ne);
        if (dateValue == null) {
            log.warn("getValue: failed to format date, null for " + customField);
        }
        return "";
    }
}

From source file:org.itracker.model.IssueField.java

/**
 * Sets the custom field value./* w ww . ja  v a2  s  .co  m*/
 * <p/>
 * <p>
 * Takes a string and then converts the value to the appropriate type based
 * on the defined field type.
 * </p>
 * <p/>
 * TODO : throw IllegalArgumentException instead of IssueException ?
 *
 * @param value  the value to set this field to as a string
 * @param locale the locale used for any string formatting
 * @param bundle the ResourceBundle used for any string formatting
 * @throws org.itracker.IssueException represents an error formatting or parsing the value
 * @deprecated locale is redundant set, in bundle and as separate parameter.
 *             use {@link IssueField#setValue(String, ResourceBundle)}
 *             instead
 */
public void setValue(String value, Locale locale, ResourceBundle bundle) throws IssueException {
    this.stringValue = null;
    this.intValue = 0;
    this.dateValue = null;

    if (value != null && value.trim().length() > 0) {
        switch (customField.getFieldType()) {

        case INTEGER:
            setStringValue(value);
            try {
                setIntValue(Integer.parseInt(value));
            } catch (NumberFormatException nfe) {
                throw new IssueException("Invalid integer.", IssueException.TYPE_CF_PARSE_NUM);
            }
            break;

        case DATE:
            setStringValue(value);
            try {
                if (null == locale) {
                    locale = bundle.getLocale();
                }
                SimpleDateFormat sdf = // CustomField.DEFAULT_DATE_FORMAT;
                        new SimpleDateFormat(
                                bundle.getString("itracker.dateformat." + customField.getDateFormat()), locale);

                Date dateValue = sdf.parse(value);
                if (dateValue != null) {
                    setDateValue(dateValue);
                } else {
                    log.error("setValue: caught exception for date " + value);
                    throw new IssueException("Invalid date.", IssueException.TYPE_CF_PARSE_DATE);
                }
            } catch (Exception ex) {
                log.error("setValue: caught exception for date " + value, ex);
                throw new IssueException("Invalid date format.", IssueException.TYPE_CF_PARSE_DATE);
            }
            break;

        default:
            setStringValue(value);
        }

    } else {
        // reset value
        setStringValue("");
        setDateValue(null);
        setIntValue(0);
    }
}

From source file:org.itracker.model.IssueField.java

/**
 * Sets the custom field value.//from   www .j  a  va 2 s  . co  m
 * <p/>
 * <p>
 * Takes a string and then converts the value to the appropriate type based
 * on the defined field type.
 * </p>
 * <p/>
 * TODO : throw IllegalArgumentException instead of IssueException ?
 *
 * @param value  the value to set this field to as a string
 * @param bundle the ResourceBundle used for any string formatting
 * @throws IssueException represents an error formatting or parsing the value
 */
public void setValue(String value, ResourceBundle bundle) throws IssueException {
    setValue(value, bundle.getLocale(), bundle);
}