Jsf application can be customized to support multiple languages.
To Implement the same we need to create Dynamic web project and make sure that project facet JSF is checked.
Now to support multiple languages create property file for different languages.
msg_en.properties for English support:
lan_title=Select Language name=Enter Your Name in English urname=Your Name is niceday=Have a nice Day.
msg_fr.properties for French support:
lan_title=Selectionnez la langue name=Entrez Votre Nom en anglais urname=Votre nom est niceday=Bonne journee.
Place the same in src folder of your project.
Create index.xhtml
<!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"> <body> <h:form> #{msg.lan_title}: <h:selectOneMenu value="#{userData.localeCode}" onchange="submit()" valueChangeListener="#{userData.countryLocaleCodeChanged}"> <f:selectItems value="#{userData.countries}" /> </h:selectOneMenu> #{msg.name}: <h:inputText value="#{userData.name}" /> <br/> <h:commandButton value="Hit" action="#{userData.actionMethod}" /> </h:form> </body> </html> For language support we need to do configuration setting in faces-config.xml: <?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2"> <application> <locale-config> <default-locale>en</default-locale> </locale-config> <resource-bundle> <base-name>msg</base-name> <var>msg</var> </resource-bundle> </application> </faces-config>
UserData bean:
import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.context.FacesContext; import javax.faces.event.ValueChangeEvent; @ManagedBean(name = "userData") @RequestScoped public class UserData { private String localeCode; private String name; private Map<String, Object> countries; public void countryLocaleCodeChanged(ValueChangeEvent e) { String newLocaleValue = e.getNewValue().toString(); System.out.println("Changing language"); for (Map.Entry<String, Object> entry : countries.entrySet()) { if (entry.getValue().toString().equals(newLocaleValue)) { FacesContext.getCurrentInstance().getViewRoot() .setLocale((Locale) entry.getValue()); } } } public String actionMethod(){ return "output"; } public String getLocaleCode() { return localeCode; } public void setLocaleCode(String localeCode) { this.localeCode = localeCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<String, Object> getCountries() { countries = new LinkedHashMap<String, Object>(); countries.put("English", Locale.ENGLISH); countries.put("French", Locale.FRENCH); return countries; } public void setCountries(Map<String, Object> countries) { this.countries = countries; } }
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_2.xsd" version="2.2"> <application> <locale-config> <default-locale>en</default-locale> </locale-config> <resource-bundle> <base-name>msg</base-name> <var>msg</var> </resource-bundle> </application> </faces-config>
Output Page
<!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" > <body> <h:form> #{msg.urname}: <h:outputText value="#{userData.name}" id = "abc"/> <br/> #{msg.niceday} </h:form> </body> </html>
Project Structure will look like:
Run application:
Change language:
Now enter name in text box and click on Hit: