Multilingual in JSF 2.0

For Multilingual we have to make property file for each Language.

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich">
    <f:view locale="#{UtilManagedBean.loc}">
        <f:loadBundle var="bid" basename="test"/>
        <head>
            <title>Testing</title>
        </head>
        <body>
            <h:form>
                <h:outputText value="#{bid.Select_Language} : " style="color: gray; font-weight: 700; font-size: smaller"/>
                 
                <h:selectOneMenu id="dropdown" value="#{UtilManagedBean.locale}">
                    <f:selectItem itemValue="en_US" itemLabel="English" />
                    <f:selectItem itemValue="hi_IN" itemLabel="Hindi" />
                    <a4j:support action="#{UtilManagedBean.changeLanguage}" event="onchange"/>
                </h:selectOneMenu>
            </h:form>
            <br/><br/>
            <center>
                <h:form>
                    <rich:panel header="#{bid.Panel_Header}" style="width: 50%">
                        <table border="0">
                            <tr>
                                <td>
                                    <h:outputText value="#{bid.User_Id} : "/>
                                </td>
                                <td>
                                    <h:inputText id="a" value="#{VaslidateManagedBean.id}"/>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <h:outputText value="#{bid.User_Name} : "/>
                                </td>
                                <td>
                                    <h:inputText  id="b" value="#{VaslidateManagedBean.name}"/>
                                </td>
                            </tr>
                        </table>
                    </rich:panel>
                </h:form>
            </center>
        </body>
    </f:view>
</html>

UtilManagedBean.java


package com.finalprac;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean(name="UtilManagedBean")
@SessionScoped
public class UtilManagedBean {

    /** Creates a new instance of UtilManagedBean */
    public UtilManagedBean() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        setLocale(request.getLocale().toString());
        System.out.println("Default Locale is : " + Locale.getDefault());
    }
    Locale loc;

    public Locale getLoc() {
        return loc;
    }

    public void setLoc(Locale loc) {
        this.loc = loc;
    }
    private String locale;

    public void setLocale(String locale) {
        this.locale = locale;
        System.out.println("Locale = " + locale);

        int last = locale.lastIndexOf("_");
        String lang = locale.substring(0, last);
        String country = locale.substring(last + 1);

        loc = new Locale(lang, country);
        setBid(ResourceBundle.getBundle("test", loc));
    }

    public String getLocale() {
        return locale;
    }
    ResourceBundle bid;

    public ResourceBundle getBid() {
        return bid;
    }

    public void setBid(ResourceBundle bid) {
        this.bid = bid;
    }

    public String changeLanguage() {
        ExternalContext ext = FacesContext.getCurrentInstance().getExternalContext();
        String srt = ext.getRequestPathInfo() + "?faces-redirect=true;";
        return srt;
    }
}

Following is to be added to faces-config.xml

    <locale-config>
        <default-locale>en_US</default-locale>
        <supported-locale>gu_IN</supported-locale>
        <supported-locale>hi_IN</supported-locale>
    </locale-config>

Below property file must be placed in class path.
test.properties

User_Id = Your Id
User_Name = Your Name
Panel_Header = Testing
Select_Language = Select Language

Display 1 :

test_hi_IN.properties

User_Id = अपनी आईडी
User_Name = आपका नाम
Panel_Header = परीक्षण
Select_Language = भाषा चुने

Display 2 :

Advertisements
This entry was posted in JSF 2.0 and tagged , . Bookmark the permalink.

2 Responses to Multilingual in JSF 2.0

  1. Med says:

    Simple class

    /**
    * @version 1.0
    * @author Med
    */
    import java.util.Locale;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.FacesContext;

    @ManagedBean
    @SessionScoped
    public class LocaleBean {

    /**
    * The Locale value is retrieved from the JSF view root.
    */
    private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

    /**
    * @return the locale
    */
    public Locale getLocale() {
    return locale;
    }

    /**
    *
    * @return The language from the locale
    */
    public String getLanguage() {
    return locale.getLanguage();
    }

    /**
    * Setter for the locale langue | It sets the locale that takes language as a parameter to the JSF View Root
    * @param language
    */
    public void setLanguage(String language) {
    locale = new Locale(language);
    FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
    }

    }

  2. Pingback: JSF odkazy | BRAIN SNIPPETS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s