0

I'm trying to implement loading Resource Bundles for JSF application from DB, following the sample: internationalization in JSF with ResourceBundle entries which are loaded from database

For the test I coded getItSomehow() just as create HashMap and fill it with key "hello_world" and value "["+locale+"]"+"hello world"

The sample works fine when I deploy it on Glassfish3. But when I use WebSphere AS 7, the jsf page is displayed correctly only for the first time. Opening the jsf page in other browsers (with other prefered language selected) I receive the respond always in the locale of first run.

While debugging, I found the difference in implementation of ResourceBundle.java: Glassfish uses this class provided in rt.jar of the JDK1.6; but WebSphere has this class inside java.util.jar

The ResourceBundle (of WebSphere) called from ApplicationResourceBundle.getResourceBundle() calls handleGetBundle() and finally invokes my.i18n.DbResourceBundle$DBControl.newBundle() . Called second (and further) time with different locale it doesn't invoke my override but just returns the same bundle created for first locale.

The question: is it possible to code internalizable jsf web-application deployed on WebSphere AS 7.0.07, not digging nor hacking into internals of the AS?

(environment: Windows XP, WebSphere AS 7.0.0.7, jdk1.6.0_24, jsf 2.1.4)

Community
  • 1
  • 1
zmila
  • 1,651
  • 1
  • 11
  • 13

1 Answers1

0

You can provide a specific implementation of ResourceBundle.

Here an example that gets the current locale each time JSF invokes the ResourceBundle methods:

package my.company.jsf.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

public class MyBundle extends ResourceBundle {

    private static final Map<Locale, ResourceBundle> RB_CACHE = new HashMap<Locale, ResourceBundle>();
    private static final String BUNDLE_NAME = "my-messages";

    public MyBundle() {
    }

    @Override
    public Enumeration<String> getKeys() {
        ResourceBundle rb = getResourceBundle();
        final Iterator<String> it = rb.keySet().iterator();
        return new Enumeration<String>() {

            @Override
            public boolean hasMoreElements() {
                return it.hasNext();
            }

            @Override
            public String nextElement() {
                return it.next();
            }

        };
    }


    @Override
    protected Object handleGetObject(String key) {
        ResourceBundle rb = getResourceBundle();
        return rb.getObject(key);
    }

    private ResourceBundle getResourceBundle() {
        Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        ResourceBundle rb = RB_CACHE.get(locale);
        if (rb == null) {
            rb = ResourceBundle.getBundle(BUNDLE_NAME, locale);
            RB_CACHE.put(locale, rb);
        }
        return rb;
    }
}

and in your faces-config.xml put:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xi="http://www.w3.org/2001/XInclude" 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">
    <application>
        <resource-bundle>
            <base-name>my.company.jsf.util.MyBundle</base-name>
            <var>MSG</var>
        </resource-bundle>
    </application>
</faces-config>

We had your same problem and this solution worked for us with Windows Server 2008, WebSphere AS 7.0.0.19, jdk1.6.0_29, jsf 2.1.5

Teg
  • 1,302
  • 13
  • 32