Question

I have a combobox where users can select an available language. The application contains properties files for each language. In the resources section of the page the resource bundle is calculates according to a language tag ( DE, EN ... ) in a user config document.

Is there any easy way to change the language in the onChange event according to the value of the combobox? I thought of context.setProperty(???). Any suggestion?

Was it helpful?

Solution

To implement this application wide you could use a phase listener. In this example, the locale to use is stored in a sessionScope variable named "Language".

Just add a combobox to your XPage(s) containing all allowed locales.

<xp:comboBox id="comboBox1" value="#{sessionScope.Language}">
    <xp:selectItem itemLabel="Chinese" itemValue="zh"></xp:selectItem>
    <xp:selectItem itemLabel="German" itemValue="de"></xp:selectItem>
    <xp:selectItem itemLabel="Turkish" itemValue="tr"></xp:selectItem>
    <xp:eventHandler event="onchange" submit="true" refreshMode="complete" />
</xp:comboBox>

Then you have to use a phase listener like this one:

package ch.hasselba.xpages.jsf.core;

import javax.faces.context.FacesContext;
import javax.faces.application.Application;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.faces.component.UIViewRoot;
import java.util.Locale;
import java.util.Map;

public class LocalizationSetter implements PhaseListener {

    private static final long serialVersionUID = -1L;
    private static final String scopeVarName = "Language";
    private static final String scopeName = "sessionScope"; 

    public void afterPhase(PhaseEvent event) {}

    public void beforePhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();
        UIViewRoot view = facesContext.getViewRoot();
        view.setLocale( getLanguage(facesContext) ) ;
    }

    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    private Locale getLanguage( FacesContext facesContext ){
        try{
            Application app = facesContext.getApplication();
            Object obj = app.getVariableResolver().resolveVariable(facesContext, scopeName );
            Object lang = ((Map) obj).get( scopeVarName );
            if( lang != null ){
                return new Locale((String) lang);
            }
        }catch(Exception e){}

        return Locale.getDefault();
    }
}

You can add lookups / etc. to access the user profiles in the "getLanguag()" method.

Hope this helps Sven

OTHER TIPS

I have hade some struggle with this in the past and have still not solved it correctly. you can set the language using

context.setLocaleString("en"); context.reloadPage();

but if you want the language to be changed depending on a user document I think you need to add the code to each beforePageLoad event you have. because the browser will always use the setting you have in the html lang=".." The resource bundle will also use the language in this setting

I have some problems with getting it to work without manual reload or reload twice. You can also try to change the lang attribute using the phaselistener

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top