简体   繁体   中英

In Java dynamically add string with properties file message?

I am able to read the message from properties file where i added all messages with key/value pair

message.template.success.equipment.create = Equipment template created successfully
message.template.success.equipment.create.termination = Termination template created successfully
message.template.success.equipment.update = Equipment template updated successfully
message.template.success.termination.update = Termination template updated successfully

for reading message from the properties file i have create a class with following code

public class PropertyReader {

    private static PropertyReader instance = null;
    private ResourceBundle bundleResource;
    private ResourceBundle bundleResourceMenu;
    private ResourceBundle bundleResourceMessage;
    private static final Logger log = Logger.getLogger(PropertyReader.class);

    private PropertyReader() {}

    private PropertyReader(Locale locale) {
        log.info("Property Reader loading files with locale : "+locale.getLanguage());
        bundleResourceMenu = ResourceBundle.getBundle("bsm-portal-menu",locale);
        bundleResource = ResourceBundle.getBundle("bsmResource",locale);
        bundleResourceMessage = ResourceBundle.getBundle("bsm-portal-message",locale);
        log.info("**** BundleResourceMessage *****");

    }

    public static synchronized PropertyReader getInstance(Locale locale) {
        if (instance == null)
            instance = new PropertyReader(locale);
        return instance;
    }


    public String getBundleResource(String propKey) {
        return this.bundleResource.getString(propKey);
    }

    public String getBundleResourceMenu(String propKey) {
        return this.bundleResourceMenu.getString(propKey);
    }

    public String getBundleResourceMessage(String propKey) {
        return this.bundleResourceMessage.getString(propKey);
    }


}

Upto this everything fine.

Now i have want to make these type of message dynamic

 message = "Entities " + appenMessage.toString() + " are already added in another association";

here appenMessage variable have a dynamically generated String value which i have to append with message.It is possible to incorporate such kind with properties file ?

Check out this tutorial about Compound Messages:

Personally, I tend to do it with simple

 bundle.getString("key").replace("{0}", firstParam);

The java.text.MessageFormat.format( pattern , argument… ) function is designed just for this purpose.

In the properties file:

message.template.entities.associated = Entities {0} are already added in another association

In your code:

MessageFormat.format(getBundleResourceMessage("message.template.entities.associated"), appenMessage)

This will cause appenMessage.toString() to be substituted for the {0} placeholder. The MessageFormat class also supports fancier variants of placeholders to support issues that you will encounter in internationalization, such as pluralization and date formatting.

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