Internationalizing Tiles Applications


In Chapter 7 you saw how to use Tiles to organize your JSP pages. The Tiles framework provides an easy way to add tiles or templates to a JSP page to present content in a dynamic fashion. The Tiles framework, just like Struts can be localized to provide different tiles based on a user ‚ s preferred locale. For example, the header tile in Chapter 7 could be replaced with a different header that corresponds to a specific locale. It could contain an area-specific flag for instance or simply a different background color .

A Tiles application has a Tiles definition file (e.g.: /WEB-INF/tiles-defs.xml ) that defines the structure of a JSP page using various tiles, for the header, menu, body, footer etc. In the case of a localized Tiles application, there will one such file per locale along with the default tiles-defs.xml file. For example, if your application supports US English and French, there will be two definition files, one for each locale as well as a default one ‚ tiles-defs_fr.xml , tiles-defs_en.xml and tiles-defs.xml

The naming conventions for the Tiles definition files are the same as for a java.util.ResourceBundle class as explained earlier in the chapter. Again, just as in a localized Struts application, the session attribute Action.LOCALE_KEY is looked up for a user ‚ s preferred or default locale and the appropriate definition file are loaded. For instance, if the default file tiles-defs.xml is:

 <tiles-definitions>   <definition name="foo.bar" path="MybankLayout.jsp">       <put name="title"  value="My Bank example" />       <put name="header" value="/header.jsp" />       <put name="menu"   value="/menu.jsp" />       <put name="footer" value="/footer.jsp" />       <put name="body"   value="/body.jsp" />   </definition> </tiles-definitions> 

Then the localized Tiles definition file for French is:

 <tiles-definitions>   <definition name="foo.bar" path="MybankLayout.jsp">       <put name="title"  value="Mon exemple de ma banque"/>       <put name="header" value="/header.jsp" />       <put name="menu"   value="/menu.jsp" />       <put name="footer" value="/footer.jsp" />       <put name="body"   value="/body.jsp" />   </definition> </tiles-definitions> 

This approach is justified if you use different JSPs per locale. However if the JSPs themselves are fully I18N capable, meaning the single JSP can adapt itself to render local sensitive UI, then the only difference between the two tiles definition for the two locales, is the title. The need for different definition files in that case could be eliminated if there was a mechanism to specify the key to the message resource bundle in the < put > element above. Unfortunately such a mechanism doesn ‚ t seem to exist at the time of writing and hence you are left with creating definitions for each locale.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net