JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Internationalization

Overview


  • Internationalization is often called I18N, for the 18 letters between I and N.
  • In the global market of software development, it is entirely possible, and most probable, that the application you write today will be running in other languages tomorrow.
  • Struts provides extensive internationalization support through
    resource bundles, interceptors and tag libraries.
  • The core functionality is provided via resource bundles.

Resource Bundles
  • ResourceBundles are used to hold the text and messages used by your application
  • The java.util.ResourceBundle is an abstract class that allows for subclasses to define localized resources
  • PropertyResourceBundle is a concrete class of ResourceBundle. This class allows
    name/value pairs to be defined in a properties file in the format name=value
  • Struts uses resource bundles to provide multiple language and
    locale options to the users of the web application.
  • Properties files named corresponding to the action class (base
    classes and interfaces) and package scopes, or as arbitrary file names.
  • The default ResourceBundle for an application is defined in the struts−config.xml
    as
    <message−resources parameter="cdmanager.ApplicationResources"/>

    where the parameter includes the package structure of where your ResourceBundle is located.
The MessageResources Class
  • The MessageResource class is part of the org.apache.struts.util package.
  •  It provides a mechanism so that you don’t have to worry about which ResourceBundle to use. By specifying a Locale, and amessage key, the MessageResource will retrieve
    the correct string for you
  • If you wanted to dynamically set the Locale for a user, you typically do it in an Action.
                java.util.Locale locale = new java.util.Locale("es");
                 setLocale(request,locale);
This changes the user’s default locale (and language) to Spanish


Example:-
  • This example shows the out first in english language and after changing the browser setting to french language.

index.jsp

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html>
<body>
  <h3>
    <bean:message key="app.title" />
  </h3>


  <html:form action="useraction" method="post">

    <table>

      <tr>
        <td><bean:message key="app.label.name" />
        </td>
        <td><html:text property="name" />
        </td>
      </tr>
      <td><html:submit value="Login" />
      </td>
      </tr>
      <html:errors />
    </table>
  </html:form>
</body>
</html>


UserForm.java
package myform;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.ValidatorForm;

public class UserForm extends ValidatorForm {

  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public ActionErrors validate(ActionMapping arg0,
 HttpServletRequest arg1
) {

    ActionErrors error = new ActionErrors();

    if (getName() == null || getName().length() == 0)
      error.add("name"new ActionMessage("app.name.blank"));

    return error;
  }

}

UserAction.java
package myaction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class UserAction extends Action {

  @Override
  public ActionForward execute(ActionMapping mapping,
 ActionForm form,

      HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    return (mapping.findForward("success"));

  }

}

ApplicationResource_en.properties.java

app.title=Struts  Internationalization 
app.label.name=User Name.
app.name.blank=<font color=red>User Name is Required</font>

label.thank=Thank You !.
 
app.submit=Login

ApplicationResource_fr.properties

app.title=Struts  Internationalisation 
app.label.name=Nomd'utilisateur.
app.name.blank=<font color=red>Le nom d'utilisateur est
exigé</font>

label.thank=Bienvenue !.
app.submit=Ouverture 

struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD
 Struts Configuration 1.2//EN"

          "http://jakarta.apache.org/struts/
dtds/struts-config_1_2.dtd"
>

<struts-config>
  <form-beans>
    <form-bean name="UserForm" type="myform.UserForm">

    </form-bean>
  </form-beans>

  <action-mappings>
    <action path="/useraction" type="myaction.UserAction"
 
name="UserForm"
      input="/index.jsp" validator="true">

      <forward name="success" path="/user.jsp" />
      <forward name="failure" path="/index.jsp" />
    </action>
  </action-mappings>

  <message-resources parameter="resource/ApplicationResource" />

</struts-config>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
xmlns="http://java.sun.com/xml/ns/javaee" xmlns
:web=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
  <display-name>StrutsJSValidator</display-name>
  <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.
ActionServlet</servlet-
class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml
</param-value>

        </init-param>
        
        <load-on-startup>2</load-on-startup>
        </servlet>
        
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    
</web-app>

user.jsp
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html>

<body>
  <h3>
    <bean:message key="label.thank" />
    <bean:write name="UserForm" property="name" />
  </h3>

</body>
</html>


Output:-
  • When we run index.jsp page.

  • Click on Login button without giving User Name.


  • Click on Login button with User Name.


  • Now Chnaging the browser settings
  • Click on  Options menu. select options as shown below..


  • Options Dialogue box will open.
  • Then click on Content tab as shown below.
  • Then click on choose button


  • Change the local to French[fr] as shoiwn below.
  • Click on Ok button.


  • Now the page changes according to language selected. as shown below.







Download Eclipse Code without lib

Download code with lib(WAR)