Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26655

How to use resource bundle files in Java code?

I've got my localized messages in a resource bundle file that the JSTL <fmt> taglib can use. Now I also want to use these messages from inside my Java code. How can it be done?

What I need is something like getMessage() for a key and getParameterizedMessage() for a key with parameters.


Update The following seems to work:

ResourceBundle messages = ResourceBundle.getBundle("messages");
String str = messages.getString("PF1");
System.out.println(str);

Object[] messageArguments = {
    "test"
};

MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(messages.getString("IT4"));
String output = formatter.format(messageArguments);
System.out.println(output);

Upvotes: 1

Views: 2864

Answers (1)

mprabhat
mprabhat

Reputation: 20323

Load your message in java.util.Properties and have a helper class which if given a key will return you the value from the Properties.

If you are looking at I18 support, the basic to start with can be found here

Upvotes: 1

Related Questions