Press enter to see results or esc to cancel.

JSF Internationalization example – JSF Tutorial Part 9

This tutorial shows hows to make your java WebProject support different languages at a time.

  1. Project structure in NetBeans IDE
  2. 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. (For English Language)

    message = Hello to United States. The Country of Donald Trump
 (For Japanese Language)

    message = こんにちは。ドナルドトランプの国
  3. JSF Managed Bean to update the page according to the user input.

    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;
    public class LangChange {
        private String languagecode;
        private static Map<String,Object> countries;
            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()) {
  4. 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"
  5. web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
             <servlet-name>Faces Servlet</servlet-name>
             <servlet-name>Faces Servlet</servlet-name>
  6. 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.

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns=""
        <title>Facelet Title</title>
           <h1>JSF internationalization example</h1><br></br>
           <h:outputText value="#{msg.message}"></h:outputText>
                 Select Language
                 <h:selectOneMenu value="#{langChange.languagecode}" onchange="submit()" valueChangeListener="#{langChange.countryLocaleCodeChanged}">
                 <f:selectItems value="#{langChange.countries}"></f:selectItems>
  7. http://localhost:8080/JSFInter/faces/index.xhtml


    After selecting the language as Japanese. The output will be,

  8. Download the project here.



Leave a Comment