n JSF application, you can change your application locale programmatically like this :
//this example change locale to france FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale('fr');
It makes JSF support for internationalization or multiple languages easily.
Complete JSF internationalization example
In this tutorial, we show you a JSF 2.0 web application, which display a welcome page, retrieve a welcome message from properties file, and change the welcome message dynamically based on the selected language.
1. Project Folder
Directory structure for this example.
2. Properties file
Here’s two properties files to store English and Chinese message.
welcome.properties
welcome.jsf = Happy learning JSF 2.0
welcome_zh_CN.properties
welcome.jsf = \u5feb\u4e50\u5b66\u4e60 JSF 2.0
Note
For UTF-8 or non-English characters, for example Chinese , you should encode it with native2ascii tool.
For UTF-8 or non-English characters, for example Chinese , you should encode it with native2ascii tool.
3. faces-config.xml
Include above properties file into your JSF application, and declared “en” as your default application locale.
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" 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" version="2.0"> <application> <locale-config> <default-locale>en</default-locale> </locale-config> <resource-bundle> <base-name>com.rsuna.welcome</base-name> <var>msg</var> </resource-bundle> </application> </faces-config>
4. Managed Bean
A managed bean, which provide language selection list , and a value change event listener to change the locale programmatically.
LanguageBean .java
package com.rsuna; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import javax.faces.event.ValueChangeEvent; @ManagedBean(name="language") @SessionScoped public class LanguageBean implements Serializable{ private static final long serialVersionUID = 1L; private String localeCode; private static Map<String,Object> countries; static{ countries = new LinkedHashMap<String,Object>(); countries.put("English", Locale.ENGLISH); //label, value countries.put("Chinese", Locale.SIMPLIFIED_CHINESE); } public Map<String, Object> getCountriesInMap() { return countries; } public String getLocaleCode() { return localeCode; } public void setLocaleCode(String localeCode) { this.localeCode = localeCode; } //value change event listener public void countryLocaleCodeChanged(ValueChangeEvent e){ String newLocaleValue = e.getNewValue().toString(); //loop country map to compare the locale code for (Map.Entry<String, Object> entry : countries.entrySet()) { if(entry.getValue().toString().equals(newLocaleValue)){ FacesContext.getCurrentInstance() .getViewRoot().setLocale((Locale)entry.getValue()); } } } }
5. JSF Page
A JSF page to display a welcome message from properties file, and attach a value change event listener to a dropdown box.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" > <h:body> <h1>JSF 2 internationalization example</h1> <h:form> <h3> <h:outputText value="#{msg['welcome.jsf']}" /> </h3> <h:panelGrid columns="2"> Language : <h:selectOneMenu value="#{language.localeCode}" onchange="submit()" valueChangeListener="#{language.countryLocaleCodeChanged}"> <f:selectItems value="#{language.countriesInMap}" /> </h:selectOneMenu> </h:panelGrid> </h:form> </h:body> </html>
6. Demo
URL : http://localhost:8080/JavaServerFaces/faces/default.xhtml
Default, locale English is display.
If user changes the dropdown box language, it will fire a value change event listener and change the application locale accordingly.
src->mkyong.com



0 commentaires:
Enregistrer un commentaire