3

I am building a web application with JSP and Java Servlet. Currently I am using JSTL fmt for the internationalization using a property file (messages.properties). But my costumer wants to be able to update text live so I need to move keys/value from the property file to the database. The problem is I don't know how I can read text from the database into for exemple <fmt:message> tag in the JSP file.

Any help is very welcome, thanks

//Momo

Momo Momo
  • 31
  • 2
  • 1
    Database queries are heavyweight; if for every caption in your web page you must do a query it will be very slow. I advice a mechanism for reloading the internationalization bundle when desired. – SJuan76 Sep 11 '12 at 17:14
  • Do you already have database access set up? You could store names and descriptions of objects with a locale oder language key in your database and let your servlet have determine the current locale. – Pao Sep 11 '12 at 22:53
  • Thanks for your answer. @SJuan76 it is true that the application will be very slow with this solution. Do you have any suggestion on how to reload the internationalization bundle when desired? – Momo Momo Sep 12 '12 at 08:01
  • @Paujo yes I have alreaday database access setup and can retrieve key/value pair. The problem is to pass them to the JSP file. – Momo Momo Sep 12 '12 at 08:14
  • Either if you read it from a file or from DB, you must cache it in a mpa (I have not used `fmt`, I usually use `ResourceBundle` which already does it). Provide a method to reload that map from source again. – SJuan76 Sep 12 '12 at 09:07
  • How do you pass data from Servlet to JSP file at the moment? `HttpServletRequest.setAttribute("attribute", "value")` should work. – Pao Sep 12 '12 at 09:39
  • Ok, I will do that @SJuan76 . Yes I am using HttpServletRequest.setAttribute("attribute", "value") to pass data to the JSP – Momo Momo Sep 12 '12 at 09:58

3 Answers3

1

<fmt:message> can make use of a LocalizationContext which, in turn, makes use of a ResourceBundle. So, really, you should focus on how to create a ResourceBundle that meets your needs. If you absolutely have to use a database, perhaps someone's already created a ResourceBundle implementation to handle that. One thing to make sure you investigate, as long as you're using at least Java 6, is ResourceBundle.Control. It may turn out that you can keep using property files but customize the caching behavior.

kschneid
  • 5,626
  • 23
  • 31
  • Yes I'am using Java 6. Using property files with a caching behavior sounds like the best solution. I will investigate the approche. Thanks – Momo Momo Sep 12 '12 at 08:08
0

Not with fmt. You either write your own tag or you change the code of fmt.

Michael-O
  • 18,123
  • 6
  • 55
  • 121
0

Could this be a solution? Instead of reading from the database för every key/value pair I read them into a map and refresh every knight. Database backed i18n for java web-app

Community
  • 1
  • 1
Momo Momo
  • 31
  • 2