Sorry - just showing off now, but I've just worked out to swap languages dynamically. First create properties files for additional languages (e.g. MessageResources_de.properties for German), then in the execute method of an Action class you can set the language to German as follows, and the tags such as <bean:message> will use the German version. The Sun website has a trail on internationalization which gives a lot more detail, but I thought this might be of use to anyone just wanting to get going quickly with multiple languages in struts.
Most sites I've seen ask the user to choose a language on the welcome page and all subsequent pages are displayed using the user's chosen language. This approach keeps the i18n issues nice and simple (the way it should be!). The posted code is very useful for changing the locale at a fork in the navigation but I think the real task of "changing languages dynamically" is more complicated. Is there an accepted, 'best-practices' way to switch languages on-the-fly using struts? Consider the scenario where every page has a 'Translate' button (or link, or img, whatever) that may be pressed at any time. When pressed, the same page must be redisplayed in the same state but with all the page's text in a new language. Also: any displayed error messages must remain and be translated; no new messages should appear; any data entered by the user must not be lost. Can this be done whitout rexecuting the action (could be expensive) that generated the translatable page? Has anybody done something similar and have tips they'd like to share? Rich Smyth [ April 01, 2004: Message edited by: Rich Smyth ]
Not sure about "the scenario where every page has a 'Translate' button", but the code above could be used in a select language action to change the language to one chosen by the user at any point, all subsequent pages would be in the chosen language.I'd guess translating the current page without rerunning the code that generated it would require code outside the scope of struts to cache results - but I've only just started looking at Struts, so don't really know. I'd also be interested in any tips on i18n as we're going to be i18nning our app at work soon. Not sure if we're going to use Struts, I'd quite like to, but it would be a lot of work for an existing webapp.It might be easier for us just to use message tags and properties files. I had thought that the following code would set the locale of the whole webapp, but have just tried it out and it works per user session, so thanks to Rick for that.