Hi Doug and others,
> The server we're creating needs to be usable simultaneously by multiple
> users in multiple languages. Our app uses Struts/JSP and the Model 2
> architecture. What is the "best practice" or the "correct" Java/Struts way
> of accomplishing this?
>
> Is there a way to use the same JSP pages, or is it best to have multiple
> contexts (/en/, /fr/, etc)?
>
Here is a brief presentation of what you can do with Struts to localize your
application.
With version 1.0, you can set the ActionServlet to do locale negociation.
Just put in you web.xml file:
<init-param>
<param-name>
locale
</param-name>
<param-value>
true
</param-value>
</init-param>
>From now, when a user connects to your application and he has defined a
locale preference in his browser, Struts will set a locale in his user
session. This locale is then used to select the appropriate message resource
file when your application displays a message through the <bean:message />
tag.
Different users can simultaneously use different languages, but generally
(with Struts framework), a user only uses one language at a time.
If you want to give to your user the ability to change the locale used, you
need to write a bit of code that changes the Locale in the user session. And
now, all the pages will use that new locale when displayed.
If you have to display pages that keep the same structure from one language
to the other, then the simpler way to localize your application is to use
the <bean:message /> tag.
Store all your strings in the appropriate resource file. Don't put
formatting in your code, but in the message strings, and use parameters. For
instance, taken from MessageFormat javadoc:
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}."
Have a look at the java.text.MessageFormat and java.util.ResourceBundle
documentation for more info. In particular, don't forget to double the
quotes ' in the messages (like ''. This one turned me crazy...).
When you want to include HTML formatting in your message other than line
breaks, it's the time to think to create different pages for the different
languages. I haven't investigated it yet, but I think that a lighter
solution that language contexts can be found with conditional include, using
the new Struts template tags.
Depending on the locale, you <template:insert /> the corresponding localized
page. This would allow a global framework for your application, but with
various looks depending on the language selected by the user.
Pierre M�tras