javaxpageslangfacescontext

How to set a user defined language in Xpages


In my xpage I need to set a user defined language before the page loads. the language to be set is stored in a document in the database

so I do this in beforePageLoad

var lang = getUserDoc().getItemValueString("Language")
facesContext.getViewRoot().setLocale(new java.util.Locale(lang));
context.reloadPage();

the problem is that if I do not do context.reloadPage the language is not set.

but context.reloadPage gives all kind of other problems when loading the page so I need to find a better way.

is there anyway I can set the language of the page without reloading the page.


Solution

  • Hope I have got it correctly, just extending my answer to Per Henrik's solution here (based on the last comment), for setting the resource bundle correctly probably you can just compute it? Something like this?

    <xp:this.resources>
            <xp:bundle var="application">
                <xp:this.src><![CDATA[#{javascript:if(context.getLocale()=="en_US")
    return "/application.properties";
    else
    return "/application_de.properties";}]]></xp:this.src>
            </xp:bundle>
        </xp:this.resources>
    

    I have just used the context variable here, but I am sure that the document variable is accessible too.

    Hope this helps.