While developing Java programs which uses ResourceBundle to load properties files, a common pratice is to put all your English locale content into the default properties file XXX.properties. Till recently I did’nt know that it is important to ship your English locale files in XXX_en.properties file also. Problem arises when the user of your program runs the
program with a different locale other than English as platform default locale.
Let’s try one example to make the problem clear.
Consider the following program,
import java.util.*;
class TestBundle {
public static void main(String args[])
{
String baseName = “MyResources”;
try {
// Get the resource bundle for a specific locale
ResourceBundle rb = ResourceBundle.getBundle(baseName, Locale.ENGLISH);
String key = “hello”;
String s = rb.getString(key);
System.out.println(s);
key = “bye”;
s = rb.getString(key);
System.out.println(s);
} catch (MissingResourceException e) {
e.printStackTrace();
}
}
}
This is a standard program used to load a resource “MyResource_en.properties”.
My expectation is that if “MyResource_en.properties” is not there the program will load
“MyResource.properties”.
We will create two properties file, “MyResource_fr.properties” and “MyResource.properties”.
MyResources.properties:
hello=Hello
bye=Goodbye
MyResources_fr.properties:
hello=Bonjour
bye=Au Revoir
Now lets run our program in the following way,
java -Duser.language=fr TestBundle
We expect Hello and Goodbye to get printed.
Oops!! but it is actually Bonjour and Au Revoir that get printed.
The secret lies in the way ResourceBundle looks for files. ResourceBundle.getBundle generates a sequence of names to find the correct translation. To explain in a simplified way, if we assume that the server or machine on which your program runs uses French locale as default locale and the your program looks up English locale, following names will be generated,
MyResource_en.propertes -> User/Program locale.
MyResource_fr.properties -> The server/machine locale.
MyResource.properties -> Default if nothing matches.
MyResource_en.propertes does not exist, so MyResource_fr.propertes gets loaded.
A more detailed explanation of the file lookup order can be found in JavaDoc.
In Solaris platform, you can set the machine default locale to fr in the following way,
In bash prompt type,
LANG=fr
LC_ALL=fr
export LANG
export LC_ALL
In windows platform use the -D option.
So the important learning is, better ship your english content both XXX.properties and XXX_en.properties files rather than tracing bugs on weekends.






arun said
hi
how to load property file from other locations
i tried following syntex but i got error (MissingResourceException)
ResourceBundle bundle=ResourceBundle.getBundle(System.getProperty(“catalina.home”)+”\\conf\\”+”arun_en.properties”);
please help me