Search code examples
javajspjstljsp-tags

Replace default resourceBundle and resourceControl implementations in JSP web application


in an existing Web Application (JSP, Struts), localizations are managed through JSTL tags fmt:setbundle, fmt:message and .properties files.

I'd like to get rid of the .properties files and use an alternative datasource for localizations.

For my goal I've created custom ResourceBundle and ResourceControl implementations (details on where data is picked, xml, database, are out of scope), but I'm wondering how to register and use them in place of the default/factory file-based implementation, so I'm not forced to modify markup code (fmt:message...) among web application files.

I saw examples that point to replace fmtResourceKey session value but it's limited to only one bundle and it looks like an "hack".

Any good ideas?

Thanks for your help!


Solution

  • Ok, it seems I sorted out with subclassing/customizing java.util.ResourceBundle, which also carries implementantion of custom ResourceBundleControl and ResourceBundleControlProvider (injected through Service Provider Interface - SPI).

    Similar solution is depicted in this page from Oracle:

    https://docs.oracle.com/javase/tutorial/i18n/serviceproviders/resourcebundlecontrolprovider.html

    but was lacking an important hint: "put your JAR inside VM" since ResourceBundle.GetBundle method internally uses Serviceloader.LoadInstalled which searches for custom provider installed inside Java VM, as stated in LoadInstalled documentation:

    This method is intended for use when only installed providers are desired. The resulting servicewill only find and load providers that have been installed into the current Java virtual machine; providers on the application's class path will be ignored.

    Thanks!