简体   繁体   中英

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM