3

I want to use the standard internationalization from JSF (in property files )and the possibility to switch to database. Is it possible to replace JSF internationalization with own implementation that retrieve the data from DB, so I can configure it ? Or is in this case another aproach better ? I've found the following example: http://jdevelopment.nl/internationalization-jsf-utf8-encoded-properties-files/ . In this example the own resource bundle class is defined. To use it only the reference in xml to implementation class is replaced.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
John N
  • 844
  • 4
  • 12
  • 20
  • 2
    possible duplicate of [internationalization in JSF with ResourceBundle entries which are loaded from database](http://stackoverflow.com/questions/4499732/internationalization-in-jsf-with-resourcebundle-entries-which-are-loaded-from-da) – BalusC Sep 09 '13 at 00:44
  • Thank you for your help! The link is very good. – John N Sep 10 '13 at 18:59

1 Answers1

3

As BalusC pointed, you need to create a ResourceBundle and register it to the app or individually per page.

Simple example:

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui">
<h:body>
    <h:outputText value="[helloworld]: #{msgs.helloworld}" />
</h:body>
</html>

faces-config.xml

<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
   version="2.0">
   <application>
      <resource-bundle>
         <base-name>com.myproject.CustomResourceBundle</base-name>
         <var>msgs</var>
      </resource-bundle>
   </application>
</faces-config>

CustomResourceBundle.java

package com.myproject;

import java.util.ListResourceBundle;

public class CustomResourceBundle extends ListResourceBundle {   
    @Override
    protected Object[][] getContents() {
        return getMapOfWordsFromDatabase();
    }

    private Object[][] getMapOfWordsFromDatabase() {
        // TODO get key and words relation from database!
        return map;
    }
}

Theory:

http://docs.oracle.com/javaee/5/tutorial/doc/bnaxv.html

http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html

dgimenes
  • 892
  • 11
  • 23