Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MissingResourceException - Can't find bundle for base name

I know that there are a lot of questions and answers exactly about this error on stackoverflow and other forums. But I still can not find the solution...

For speed reasons I have one utility class which loads all static data maps (for example months) depending on locale provided.

So this utility class looks something like this:

public static final String GLOBAL_MESSAGES = "globalMessages";

private static Map<Integer,Month> monthsMap;

private ResourceBundle getResourceBundle(Locale locale) {
    ResourceBundle rb = ResourceBundle.getBundle(GLOBAL_MESSAGES, locale);
    return rb;
}

private Map<Integer,Month> getMonths() {
    if(monthsMap == null) {
        setMonths();
    }
    return monthsMap;
}

private void setMonths() {
    try {
        monthsMap = getFactory().getDAO().getAllMonths();
    } catch (SQLException e) {
        logger.error(e);
    } catch (EmptyResultException e) {
        logger.error(e);
    }
}

public Map<Integer,Month> getMonths(Locale locale) {
    if(locale == null) {
        return monthsMap;
    } else {
        if(this.locale != locale) {
            this.locale = locale;
            setMonths();
        }
    }
    ResourceBundle rb = getResourceBundle(locale);
    Map<Integer,Month> map = new HashMap<Integer, Month>();
    for(Month akVO : getMonths().values()) {
        try {
            akVO.setName(rb.getString(akVO.getName()));
        } catch (MissingResourceException e) {
            //already done
        }
        map.put(akVO.getId(), akVO);
    }       
    return map;
}

Files globalMessages.properties (globalMessages_en_US.properties,...) are directly in source package resources. When deployed on Tomcat there are in folder WEB-INF/classes.

Now the problem. It all works when working in this application. But I have another application which connects throught REST API (JAX-RS) to this one. When making a request App/rest/months.xml I get the following error:

java.util.MissingResourceException: Can't find bundle for base name globalMessages, locale en_us

I am really lost. And desperate...

like image 725
Trick Avatar asked Jul 23 '10 13:07

Trick


2 Answers

Ok... Found the error. After one f* day... The problem is in case sensitive letters. Even though, when setting the locale from rest with "en_US" somehow ResourceBundle (when going through REST) is looking for "en_us".

EDIT: Ok, found also the error why it was all in small letters. Problem was, because I was creating locale with:

Locale locale = new Locale("en_US");

instead of:

Locale locale = new Locale("en","US");
like image 69
Trick Avatar answered Oct 16 '22 00:10

Trick


For anybody having trouble with a Glassfish server in this case, there is the possibility to set the Locale within the Admin Overview

enter image description here

Simple overwrite the default locale as shown in the picture

Somebody might find this useful, I had trouble for multiple hours and dont want anyone to have this kind of problem aswell

like image 25
Snackaholic Avatar answered Oct 16 '22 02:10

Snackaholic