Jan de Rijke wrote:

> Anyone found an elegant way of doing multilingual jsp
> or should I stick to servlets for this?
>
> tnx,
>

This is a case where the "redirecting requests" application design model
(see Section 1.6.5 of the JSP 1.0 spec) has helped me out.  I'm building
a multilingual application where the processing logic is the same (and
it is language-sensitive, based on a java.util.Locale object I store in
the users's session).  The basic approach I've taken is this:

* There is a version of each JSP page in each language,
  named according to a consistent pattern.  For example,
  my main menu is in "menu_en.jsp", "menu_fr.jsp",
  "menu_nl.jsp", and so on.

* When the user logs on, one of the things I store in the
  session is a java.util.Locale object that describes the user's
  desired language.  (I also use this to get correct date formats
  and other stuff, courtesy of Java's internationalization support).

* Each form submission (no matter what language) goes to a
  single servlet that performs the required processing, creates or
  updates beans in the user's session to reflect the results, and
  then forwards to the language-sensitive version of the next
  JSP page to be called.  Because the processing logic is already
  language sensitive, I did not need to make five (for now) copies
  of it in five JSP pages, and then face the maintenance headaches
  when I changed the logic later.

* In order to calculate the right page, my servlet has a convenience
  method to dynamically calculate the path, something like this (error
  handling is excluded for brevity):

    public String jspPath(HttpServletRequest req, String baseName) {
        HttpSession session = req.getSession();
        Locale locale = session.getValue("locale");
        return (baseName + "_" + locale.getLanguage() + ".jsp");
    }

* I can use this convenience method within a servlet to pass control
  to the correct page, something like this:

    RequestDispatcher rd =
        getServletContext.getRequestDispatcher(jspPath(req, "/menu"));
    rd.forward(req, res);

* When I need to look up text (such as an error message) in a servlet,
  I use a wrapper around a java.util.ResourceBundle class that looks up
  the user's locale in the same way, to select the correct version of
the
  message.

This whole approach has made dealing with the languages stuff totally
transparent to the processing logic.  In fact, I was able to add dynamic
language switching to every page of the app pretty simply, by creating a
link to a servlet that just changes the locale (stored in the session),
followed by a return to the same page it came from, with almost no pain
at all.


>
> jan.
> --
>        Jan de Rijke          | Uniway
> email: [EMAIL PROTECTED] | L. Marelaan 12/1
> voice: +32-2-7136543         | B-1932 St Stevens Woluwe/Belgium
> fax:   +32-2-7136541         | www.uniway.be

Interesting ... our subsidiary division that I'm building this for is
just a few kilometers from Woluwe, in Brussels itself.

Craig McClanahan

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to