Search code examples
jsfinternationalizationjava-ee-7

How to load messages to resource bundles from a database and bind strings via message keys to the view


Currently I'm developing a Java 8 / JavaEE 7 Web application using JSF 2 with Primefaces 6.

As a part of this project, I have to build a module to fetch translations (messages in German and English) from a database to get a dynamically translated application since hard coding Strings simply isn't an option.

After two days of reading stuff about ResourceBundles, ResourceBundle.Controls and ResourceBundleControlProvider I completely lost understanding of the whole matter.

I found many possible solutions that were about two to three years old, which were always completely different, which is why I came to ask my question here. I know that there are some posts here that describe some cases but I would be glad if someone could help me understand this matter.

Could anyone explain the usual workflow or state-of-the-art method to solve my problem and give an explanation of the important classes used for the solution?


Solution

  • If anyone ever encounters the same issue as me, after days of searching and tinkering around I found a website that provides a very neat tutorial to achieve i18n in a JSF application with localized strings from a database.

    This guide explains the needed steps one after another and worked pretty well for me. https://zenidas.wordpress.com/recipes/database-resource-bundle-in-a-jsf-application/

    The basic steps to be done are as follows:

    • Create the resource bundle extension that will delegate the resources lookup in a database control
    • Create the necessary extensions for different locales
    • Create the database tables and the corresponding JPA entities
    • Create the resource bundle control that will get the contents from the database
    • Define the resource bundle in the faces-config file

    After those steps it should be possible to access the localized data from the database, if there are some difficulties I'd recommend to test the database access itself independent of the resource bundles.

    Hopefully this will be useful to someone, someday.