Friday, October 22, 2010

Spring with in a Day Part-5

Internationalization (i18N)
----------------------------------------------
i. If an application can takes care of the differences the language, number format, date format, currency symbol etc that exist between different regions, then the application can called as internationalization application.
ii. Every almost operating system allows us to specify the regional setting or local settings like date format, language etc.
iii. The browser sends accept-language header as part of request. This header contains the local settings of the browser.



iv. We can use the code shown bellow for identifying the local setting of the browser –
< % java.util.Local loc;
Loc = request.getLocal();
out.print(loc,+”< br>”);
out.print(loc.getLanguage() +”< br>”);
out.print(loc.getCountry() +”< br>”);
%>
//checkLocalSettingofBrowser.jsp

v. request.getLocal() uses the value of Accept-Language header for creating the local object.
vi. As part of contain that send to the browser there will be parts that are not specific to the local and there are few parts are specific to the local.
abc.html (sjp for generating output en-us local)
< html>
< head>< title>Title en< /title>< /head>
< body>
< p> Para 1 en < /p>
< p> Para 2 en< /p>
< p> Para 3 en< /p>
< /body>
< /html>




vii. Providing multiple JSP for supporting different local as shown above complicate the development and the maintenance the application.
xyz.html (jsp for generating output fr-ca local)
< html>
< head>< title>Title fr< /title>< /head>
< body>
< p> Para 1 fr < /p>
< p> Para 2 fr < /p>
< p> Para 3 fr < /p>
< /body>
< /html>




viii. We should not place local specific resources in the JSP. There resources must be place in properties files.
ix. It is not recommended to place the html tags that are not specific to a local in properties files.

Procedure for implementing i18N application with Spring
----------------------------------------------------------------------------
i. Add the poperties files to WEB-INF/classes directory.
ii. Procedure for adding the properties files to the project –
 Select src directory in the package explorer
 Right click to get the popup menu
 Choose New => File
 In new file window provide the filename (resone.properies) and click on Finish button.

Note: MyEclipse creates resone.properties file in src directory and copied in classes directory.

Property file resone.properties :-
pone.title = Welcome in page 1
pone.prone = Para 1 in page 1
pone.prtwo = Para 2 in page 1
pone.prthr = Para 3 in page 1
pmsg = Copy file from {0} to {1} in page 1

Property file resone_fr.properties :-
pone.title = Welcome in page 1 for fr
pone.prone = Para 1 in page 1 for fr
pone.prtwo = Para 2 in page 1 for fr
pone.prthr = Para 3 in page 1 for fr
pmsg = Copy file from {0} to {1} in page 1 for fr

Property file restwo.properties :-
ptwo.title = Welcome in page 2
ptwo.prone = Para 1 in page 2
ptwo.prtwo = Para 2 in page 2
ptwo.prthr = Para 3 in page 2
pmsg2 = Copy file from {0} to {1} in page 2

Property file restwo_fr.properties :-
ptwo.title = Welcome in page 2 for fr
ptwo.prone = Para 1 in page 2 for fr
ptwo.prtwo = Para 2 in page 2 for fr
ptwo.prthr = Para 3 in page 2 for fr
pmsg2 = Copy file from {0} to {1} in page 2 for fr





iii. Configure ResourceBundleMessageSource bean in applicationContext.xml as shown bellow -
< bean id=”messageSource”
class=”org.springframework.context.support.ResourceBundleMessageSource”/>
< property name=”basenames” value=”resone,restwo”>< /property>
< /bean>

 The internal code of ResourceBundleMessageSource reads the properties file and places the info in the memory.
 If we place resources with the code XXX in resone and restwo then spring used the value of XXX available in resone.
 Use basename instead of basenames if you use single resource set in the application.

How use Message Resource in JSP
--------------------------------------------------
For using the resources from the JSP we must use input spring tag library and use the message tag as shown bellow –

JSP code (genOP.jsp):-
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
< %@ page isELIgnored=”true” %>
< html>
< head>< title>< spring:message code=”pone.title”/>< /title>< /head>
< body>
< p>< spring:message code=”pon.prone”/>< /p>
< p>< spring:message code=”pon.prtwo”/>< /p>
< p>< spring:message code=”pon.prthr”/>< /p>
< p>< spring:message code=”pmsg” arguments=”dirOne,dirTwo” />< /p>
< /body>
< /html>

dirOne will be used in place of {0} and dirTwo will be used in place of {1}. message tag of spring tag library checks the locale the browser that has sent the request and picks up the appropriate value for the resource and send it to the browse.

No comments:

Post a Comment