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

  • Struts2 provides extensive internationalization support through
    resource bundles, interceptors and tag libraries.
  • The core functionality is provided via resource bundles.

Resource Bundles
  • Struts2 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.
  • To find a keys value, the properties files are searched in the following order until the key is found:
    • The properties file for the action class – i.e. MyAction.properties
    • The properties file for each base class in the action hierarchy, all the way up to Object.properties.
    • A properties file for each interface and sub-interface.
    • A properties file called “package.properties” in each package from the action back up to the root package.
    • Properties files configured in “struts.properties” under the “struts.custom.i18n.resources” configuration key.
Interceptors and Determining the Locale

  • By default, Struts2 will set the users locale for the session from the HttpServletRequest object..
  • This comes directly from the web browser, and is based on the Accept-Language HTTP header.
  • When a web application needs to present content in multiple languages that are not dependant on the locale of the web browser, the “i18n” interceptor can be used.
  • This interceptor checks for a request parameter called “request_locale”, and saves this information to the user session.
  • until it is changed by the request parameter again, the specified locale will
    remain as the locale for the remained of the user’s session.
Tag Libraries

  • There are two way to programmatically obtain internationalized text for a page.
  • Both require the action to extend ActionSupport so that the necessary internationalization methods are available.
  • The first is by using the “text” tag, which searches for text for
    the key provided by the “name” attribute:
<s:text name="label.greeting"/>
<s:text name="label.greeting2">
<s:param >Mr Smith</s:param>
</s:text>

The examples above would correspond to
the following properties file entries
label.greeting=Hello there!
label.greeting2=Hello there {0}!


The second way of obtaining the text values is by using OGNL
methods and the “property” tag.


<s:property value="getText(‘label.greeting’)"/>
<s:property value="getText(‘label.greeting2’)">
<s:param >Mr Smith</s:param>
</s:text>

Because the “label” attribute is not by default an OGNL expression, we need to use the “%{“ and “}” token to force Struts2 to interpret it as an expression.


<s:textfield label="%{getText(‘label.greeting’)}"/>

The i18n tag provides a way to provide additional resource bundles to the value stack of the page being rendered, the “name” attribute providing the name of the resource bundle.
Any tag contained within the “i18n” tag will then have access to the new resource bundle text.


Example:-

UserAction.properties
app.title=Struts Internationalization 
app.label.name=User Name.
app.label.age=Age.
app.label.salary=Salary.
app.name.blank=User Name is Required
app.sal.blank=Salary cannot be left Zero
app.age.blank=Age cannot be left Zero
app.age.invlid=Age must be in between 18 and 60
label.thank=Thank You !.
label.login.age=Your Age : 
label.login.sal=Your Salary : 
app.submit=Login

UserAction_fr.properties
app.title=Struts Internationalisation 
app.label.name=Nomd'utilisateur.
app.label.age=ces.
app.label.salary=être.
app.name.blank=Le nom d'utilisateur est exigé
app.sal.blank=Le nom d'utilisateur sal exigé
app.age.blank=Le nom d'utilisateur age exigé
app.age.invlid=Nom ou mot de age inadmissible d'utilisateur
label.thank=Bienvenue !.
label.login.age=Vous est : 
label.login.sal=Vous sal : 
app.submit=Ouverture 

index.jsp
<%page contentType="text/html; charset=UTF-8" %>
<%taglib prefix="s" uri="/struts-tags" %>
<html>
   <body>  
   <h1><s:text name="app.title"/></h1>
   Enter User Details:
     <s:form action="useraction" method="post">
           <s:textfield key="app.label.name" name="name"/>
           <s:textfield key="app.label.age" name="age"/>
           <s:textfield key="app.label.salary" name="salary"/>
           <s:submit key="app.submit"/>
     </s:form>
   </body>
 </html>


user.jsp
<%page contentType="text/html; charset=UTF-8"%>
<%taglib prefix="s" uri="/struts-tags"%>
<html>

<body>
  <s:text name="label.thank" />
  <b><s:property value="name" />
  </b>.
  <br>
  <br>
  <s:text name="label.login.age" />
  <b><s:property value="age" />
  </b>
  <br>
  <br>
  <s:text name="label.login.sal" />
  <b><s:property value="salary" /> $</b>
</body>
</htm


struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.custom.i18n.resources" value="myaction.UserAction"/>
  <package name="default" extends="struts-default">
    <action name="useraction" class="myaction.UserAction">
      <result name="success">/user.jsp</result>
      <result name="input">/index.jsp</result>
    </action>
  </package>
</struts>  


UserAction.java
package myaction;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

  String name;
  int age;
  int salary;

  public String getName() {
    return name;
  }

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

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getSalary() {
    return salary;
  }

  public void setSalary(int salary) {
    this.salary = salary;
  }

  public void validate() {
    if (getName() == null || getName().length() == 0)
      addFieldError("name", getText("app.name.blank"));
    if (getAge() == 0)
      addFieldError("age", getText("app.age.blank"));

    if(getAge()<=18 || getAge()>=60)  
        addFieldError("age", getText("app.age.invlid"));
    if (getSalary() == 0)
      addFieldError("salary", getText("app.sal.blank"));

  }
  public String execute() throws Exception {

    return SUCCESS;
  }
}

Download Source without lib

Download War with lib

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






Chnaging Language to French....











Another way to change the Language is changelanguage?request_locale
request_locale parameter..!!

We have jsut added two lline of code in index.jsp file as

 
<h3><a href="changelanguage?request_locale=fr">View in Franch</a></h3>
<h3><a href="changelanguage?request_locale=en">View in English</a></h3>

And in struts.xml file  added the mapping  of  change language request.


<action name="changelanguage">
      <result>/index.jsp</result>
    </action>


Output:-




Download Source without lib  
Download War with lib