Re: I18N and locales
David Graham [EMAIL PROTECTED]: Cool, but how do I i18n the text so that I can put it in the request? Put an init-param element inside the servlet element of the web.xml file of the servlet, referring to the Struts Action servlet, that refers to a resource bundle. Eg. servlet servlet-nameaction/servlet-name servlet-classorg.apache.struts.action.ActionServlet/servlet-class init-param param-nameapplication/param-name param-valueMyResources/param-value /init-param This will refer to files named /WEB-INF/classes/MyResources.properties /WEB-INF/classes/MyResources_no.properties /WEB-INF/classes/MyResources_en_GB.properties The .properties files contains key/value pairs, where the keys are the keys given to bean:message and the values are what is shown. In the example above _no designates a file with Norwegian values, and _en_GB designates a file with UK English values. The file named MyResources.properties contain the default values. -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: I18N and locales
On Thu, 8 Aug 2002, David Graham wrote: Date: Thu, 08 Aug 2002 13:21:47 -0600 From: David Graham [EMAIL PROTECTED] Reply-To: Struts Users Mailing List [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: I18N and locales When using the bean:message tag do you have to supply a different locale key to change the language it uses? I was under the impression that struts handles this automatically but reading the docs again seemed to indicate otherwise. I use the tag like this: bean:message key=button.add/ If a browser hits the page and specifies a different locale than english, does struts select the right resource file or do I have to do something? Struts manages the choice of language by maintaining a Locale object under a well-known session attribute key (Action.LOCALE_KEY). If there isn't one already, it is set automatically based on the browser's Accept-Language header. However, your app can replace the default one -- for example, if you offered a select language option. Also, in one of my actions, after I save info to a database, I want to put a message in the request before I forward to the success page. The success page would display whatever message it receives. What struts class do I use in java code to retrieve i18n messages like the bean:message tag? If you have internationalized the text of the message already, you can just store the String as a request or session attribute, and display it with the bean:write tag. For example (assuming you stored the text as a request attribute named foo): bean:write name=foo/ If you want the output page to internationalize the text (by looking it up in your application resources), store the message *key* as a request or session attribute, and use that as the argument to a bean:message call on the destination page. For example (assuming you stored the correct key as a request attribute named foo): bean:message key='%= (String) request.getAttribute(foo) %'/ Thanks, Dave Craig -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: I18N and locales
Thanks for the info Craig! More questions below... If you have internationalized the text of the message already, you can just store the String as a request or session attribute, and display it with the bean:write tag. For example (assuming you stored the text as a request attribute named foo): bean:write name=foo/ Cool, but how do I i18n the text so that I can put it in the request? My Action class' code needs to look up a key in the appropriate resource file. What struts class would I use to do this? I could just as easily do it the other way you suggested but I would like to know if this is possible. Thanks, Dave _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: I18N and locales
On Thu, 8 Aug 2002, David Graham wrote: Date: Thu, 08 Aug 2002 13:41:48 -0600 From: David Graham [EMAIL PROTECTED] Reply-To: Struts Users Mailing List [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: I18N and locales Thanks for the info Craig! More questions below... If you have internationalized the text of the message already, you can just store the String as a request or session attribute, and display it with the bean:write tag. For example (assuming you stored the text as a request attribute named foo): bean:write name=foo/ Cool, but how do I i18n the text so that I can put it in the request? My Action class' code needs to look up a key in the appropriate resource file. What struts class would I use to do this? I could just as easily do it the other way you suggested but I would like to know if this is possible. I suggest reading the JavaDoc comments about the global constants in org.apache.struts.action.Action -- you'll find out a lot of stuff about where Struts stores all of its internal data structures. If you need access to the message resources from your Action, that's straightforward as well: MessageResources resources = (MessageResources) getServlet().getServletContext().getAttribute(Action.MessagesKey + appPrefix); String text = resources.getMessage(my.key); If you have more than one message resources defined for your subapp, the extra ones are stored under whatever attribute name you specified with the bundle attribute. Of course, if you followed my second suggestion and just passed the key, instead of the internationalized text (and used bean:message in the destination page), you wouldn't have to do this. Thanks, Dave Craig -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]