I'm trying to display some message on the screen in a different language (but keeping the dates in the default language, uk_eng), depending on what user is looking at the screen. Being only a temporary setting I was wondering what's the best way to do it in Java.
+3
A:
You could have message bundles for each Locale. Load these and display them appropriately when you identify the user's Locale.
An example is at http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/int.html
You could load these in a web app too like http://www.devsphere.com/mapping/docs/guide/internat.html
ktaylorjohn
2010-08-23 10:56:52
A:
If I see the problem well, you want to display messages with MessageFormat
like this:
Object[] arguments = {
new Integer(7),
new Date(System.currentTimeMillis()),
"a disturbance in the Force"
};
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
arguments);
(Example from javadoc)
I checked the source of the MessageFormat
and I see that getLocale()
is common for the whole message. You cannot make a distinct one for a parameter.
Why don't you make a parameter with the formatted date string itself? Like this:
Object[] arguments = {
new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa", Locale.UK).format(new Date())
};
String result = MessageFormat.format(
"This is the date format which I always want independently of the locale: {1} ",
arguments);
The first parameter of the format methods may come from localized property files.
pcjuzer
2010-08-23 11:23:46
The issue was more about the resource to use depending on the different user wanting to see them, not about how to format dates with a hard coded locale. the simpleDateFormat will always use the default locale. but the text will use a specific resource bundle. thanks
Marc
2010-08-24 14:34:21