JSF Internationalization example – JSF Tutorial Part 9
This tutorial shows hows to make your java WebProject support different languages at a time.
- Project structure in NetBeans IDE
- You should have JSf resource bundle properties file for all the languages you need in the project. For eg if you are having English and Japanese language in your project. You should have properties file for both the languages.
message.properties (For English Language)message = Hello to United States. The Country of Donald Trump
message_ja.properties (For Japanese Language)
message = こんにちは。ドナルドトランプの国
- JSF Managed Bean to update the page according to the user input.
LangChange.javapackage com.chillyfacts.com; 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 @SessionScoped public class LangChange { private String languagecode; private static Map<String,Object> countries; static{ countries= new LinkedHashMap<String, Object>(); countries.put("English", Locale.ENGLISH); countries.put("Japanese", Locale.JAPANESE); } public void setCountries(Map<String, Object> countries) { LangChange.countries = countries; } public Map<String, Object> getCountries() { return countries; } public void setLanguagecode(String languagecode) { this.languagecode = languagecode; } public String getLanguagecode() { return languagecode; } public LangChange() { } public void countryLocaleCodeChanged(ValueChangeEvent take_event){ String new_language_code = take_event.getNewValue().toString(); for (Map.Entry<String, Object> entry : countries.entrySet()) { if(entry.getValue().toString().equals(new_language_code)){ FacesContext.getCurrentInstance() .getViewRoot().setLocale((Locale)entry.getValue()); } } } }
- Configure the faces-config file. Specify your default language and default properties as shown.
<?xml version='1.0' encoding='UTF-8'?> <faces-config version="2.2" 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"> <application> <locale-config> <default-locale>en</default-locale> </locale-config> <resource-bundle> <base-name>com.chillyfacts.com.message</base-name> <var>msg</var> </resource-bundle> </application> </faces-config>
- web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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-app_2_5.xsd"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list> </web-app>
-
By default it will be in English Language as we set in faces-config page. The drop down will submit the form on selecting the value and it will update the language as per the properties file.
index.xhtml<?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://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"> <h:head> <title>Facelet Title</title> </h:head> <h:body> <h1>JSF internationalization example</h1><br></br> <h:outputText value="#{msg.message}"></h:outputText> <h:form> Select Language <h:selectOneMenu value="#{langChange.languagecode}" onchange="submit()" valueChangeListener="#{langChange.countryLocaleCodeChanged}"> <f:selectItems value="#{langChange.countries}"></f:selectItems> </h:selectOneMenu> </h:form> </h:body> </html>
-
http://localhost:8080/JSFInter/faces/index.xhtml
index.xhtmlAfter selecting the language as Japanese. The output will be,
-
Download the project here.
JSFInter
0 Comments
Comments
Leave a Comment