To reach the broadest audience, some applications support more than one language. Even if your application supports a single language, message resources are an effective tool for managing shared phrases throughout the application.
Presentation - Localizing Content
How can we internationalize applications? Which Struts tags are message-aware? Can we localize dates and numbers?
We overview the localization features, which are considered a fundamental part of the framework.
- Resource Bundles
- Struts Message Tags
- Formatting Messages
Workshop - Localize
Story: In addition to English, MailReader must support other languages, including Japanese and Russian.
- Localize the MailReader application using a standard Resource Bundle
Prerequisites
- text tag
- getText method
- <s:message key="" /> idiom
Exercises
- Copy the resources*.property files from "localize" on the CD
Pages
- Localize the MailReader page by replacing static strings with references to an entry in the message resources.
- <s:text name="index.title"/>
- <s:textfield key="username"/>
Classes
- Localize the MailReader classes by calling getText with a message key, instead of passing a static string.
Validations
- Localize the validation configuration files by changing the message elements to use a key
- <message key="error.username.required"/>
Hint Code
- On each page, use this markup before the <head> section
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib uri="/struts2" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- On the Welcome page, use code like this to switch between each language.
- The code for Russian is "ru". Japan is "ja".
<li> <s:url id="en" action="Welcome"> <s:param name="request_locale">en</s:param> </s:url> <s:a href="%{en}">English</s:a> </li>
Here, we are appending the parameter "request_locale=en" to tell the framework which language to use.
| The Japanese locale may not work under Tomcat 5.5 |
Accomplishments
- Localized the MailReader application using a standard Resource Bundle
Extra Credit
- Create a Selenium test that switches from English to French and back.
