JavaSercer Pages The next action in Example 11.1 is . This is described in Table 11.5. Table 11.5, Attributes for Attribute Name Java Type Request-Time Value Accepted Description name String No Mandatory. The name of the LocaleBean instance. key String Yes Mandatory. The name of a property in the text resource properties files. The action is used to get the page title and header. The name attribute specifies the name of the LocaleBean created by the action, and the key attribute specifies one of the properties in the files with localized strings. These files are named exactly like the files used by the ResourceBundle described in the previous section. In other words, you need one file with the same name as the base name (specified as the bundleName for the action) for the default locale, and one file with a name that combines the base name and a language code for all other locales. In this example, then, you need the files poll.properties, poll_de.properties, and poll_sv.properties. If you want to add support for another language, say Italian, just create a poll_it.properties file and add it (the language code for Italian) to the list of supported languages for the action. All properties files must be placed in the WEB-INF/classes directory for the web application so that the ResourceBundle can find them. Here’s what the poll.properties file looks like: poll.title=Industry Trends poll.select_language=Select your preferred language poll.new_language=New Language poll.english=English poll.swedish=Swedish poll.german=German poll.question=What’s the longest development time you dare to plan with? poll.answer1=One year poll.answer2=Six months poll.answer3=Less than six months poll.submit=Vote poll.number_of_votes=Total number of votes poll.result=Poll result The value of the poll.title key, used by the first two actions, is set to “Industry Trends”; that’s what will appear as the title and header of the page when the default locale is selected. If a Swedish locale was selected instead, the text “Industri Trender” would be used, which is how it is listed in the poll_sv.properties file. The action is used with different keys for all text content in the page. Internally, it uses one of the bean’s regular methods: public String getText(String propertyName) This method returns the specified property (the action element uses the key attribute value) from the properties file that most closely matches the selected locale. The bean provides similar methods for date and numeric values, as you can see in Appendix C. To let the user pick another language than the one selected based on the Accept-Language header, the page contains a form with a set of radio buttons and a Submit button. Every time the page is displayed, the radio button group must reflect the currently selected language. This is done by calling the bean’s language property access method and comparing the return value with the language code represented by each radio button: … > <%= locale.getText("poll.english") %> … You probably recognize the type of JSP expression used to set the checked attribute for the radio button from previous chapters. The getLanguage( ) method returns the language code for the selected locale as a String. The equals( ) method compares the return value to its argument and returns true if they are the same. If they are, the first string after the question mark is returned as the value of the expression. If not, the second string is used. You also may have noticed that you can use the bean’s getText( ) method directly, as an alternative to the action. Which alternative to use is largely a matter of preference. I used the method here because it’s more compact and less intrusive when the text is used as part of an HTML element. page 155
Note: If you are looking for good and affordable webspace to host and run your servlet application check Sandzak servlet hosting services
This entry was posted
on Sunday, December 31st, 2006 at 1:05 pm and is filed under Java.
You can follow any responses to this entry through the RSS 2.0 feed.
Responses are currently closed, but you can trackback from your own site.