If you search the web, you’ll find a couple of solutions to change the language of your web application. I like to contribute an other one, which might be the shortest 😉

Suppose, you want to display all supported languages in their native tongue.   Thus, you need no translation for the language names. The language codes are simply passed as parameter to your action.

Here is what you need in your JSF page (feel free to replace commandBotton by commandLink or something else):

<h:commandButton action="#{tool.changeLang('de')}" immediate="true" value="Deutsch"/>
<h:commandButton action="#{tool.changeLang('en')}" immediate="true" value="English"/>
<h:commandButton action="#{tool.changeLang('fr')}" immediate="true" value="Français"/>

And in Tool.java you need this short method:

public String changeLang(String langCode) {
       FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale (langCode));
       return null;
}

That’s all.

 

To web development content.