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.
Do you want to support this blog? You may help to finance the needed hardware.
Want to read more about software development? Purchase one of my books:
Start application development with Java. Learn development foundation, quality control and more.
A Journey through Java EE Technologies whilst developing Web Applications with JavaServer Faces.
Java Lambdas and Parallel Streams
The compact starter: Foundation, supporting structures, parallel processing
No software development, just nature:
A photographic image book. Get it for free. Or pay whatever you like.
This was very helpful. Thank you so much!