3

Background

I am developing a Java Web Application using Maven. I have created ResourceBundles in this path and format: src/main/resources/i18n/LanguageBundle_xx_XX.properties.

Everything works perfectly and I have everything working on the front end (using JSTL).

Problem

The user can create KEY/VALUE pairs, from the front end, that are then stored in a MySQL database in the format: KEY | VALUE

I have a initializing servlet that does all the setting up before taking the user to the first jsp page.

In that initializing servlet I am getting all the KEY/VALUE pairs from the database, but now I want to add them to the LanguageBundle so that translations are ready when the first jsp page is presented.

I've tried so far:

  1. Returning all the key/value pairs from the DB
  2. Iterating the values returned
  3. If KEY doesn't exist in LanguageBundle_xx_XX
  4. Add the KEY/VALUE with property.put("KEY","VALUE")

Now, that does add the property, however if I try to access that specific property outside that scope (e.g., other servlets, jsp), it fails as it doesn't exist in the specific .properties file.

How do I solve this problem?

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
RasmusF
  • 41
  • 4
  • What you have created so far is not a ResourceBundle but a properties file. You need to look into the various ways of creating a ResourceBundle, which is an *object*. – user207421 Aug 15 '16 at 22:39
  • I feel @EJP idea has some merit. I had the same. The problem is not the property files, the problem is creating the resourcebundle such that it not only reads from file, but also from database. Maybe look at http://stackoverflow.com/questions/2451049/do-resource-bundles-in-java-support-runtime-string-substitution ? – Koos Gadellaa Aug 15 '16 at 22:55

1 Answers1

1

UPDATE:

I have come up with a solution that works.

Properties prop = new Properties();
    InputStream in = getClass().getResourceAsStream("/i18n/LanguageBundle_en_US.properties");
    prop.load(in); 
prop.setProperty("KEY", "VALUE");
in.close();
prop.store(new FileOutputStream(getClass().getClassLoader().getResource("/i18n/LanguageBundle_en_US.properties").getFile()), null);

Can someone tell me if this is bad practice or is it fine to use?

Thank you.

RasmusF
  • 41
  • 4
  • You are overwriting the existing file which might be bad for the next time. I think you should try to find a way to modify the values in memory. – pamcevoy Aug 15 '16 at 22:44
  • Thank you for getting back to me. I do overwrite the actual .properties file, however, it only happens in the target folder. Those KEY|VALUE pairs are the dead last and only properties I will ever add. I can see your concern. I will leave it working for now and get back to it at a later stage. If I come up with a better solution I will update my answer. Have a nice day. – RasmusF Aug 16 '16 at 08:17
  • If you are using Spring, you can use `org.springframework.core.io.support.PropertiesLoaderUtils` which does the same. – Tomer A May 16 '17 at 16:16