As default, I18N Struts-applications are managed by the language in the browser. I would like it to be based on a language-code in the users login and thus I need to set the locale/language designation that controls Struts. .

Ted Husted

As far as I know, by default, Struts applications are governed by the default locale of the container/JVM hosting the application. (The last thing Struts is going to do is trust what some whacky browser has to say about it! [:0])

The controller creates a default locale object and places it in the session. The other components look for the object under that key to decide what locale to use. To change the locale, change the locale object in the session.

It can be convenient to put methods to access the locale object on a base Action or base ActionForm:

Action:

  protected Locale getLocale(HttpServletRequest request) {

        Locale result = null;
        HttpSession session = request.getSession();
        if (session!=null) {
            result = (Locale) session.getAttribute(Action.LOCALE_KEY);
            if (result == null) result = Locale.getDefault();
        } else {
            result = Locale.getDefault();
        }

        return result;

    } // end getLocale()

    protected void setLocale(
            HttpServletRequest request,
            Locale locale) {

        HttpSession session = request.getSession(true);
        session.setAttribute(Action.LOCALE_KEY,locale);

    } // end setLocale()

ActionForm:


    public static String STRUTS_LOCALE_KEY = Action.LOCALE_KEY;

    private Locale locale = null;

    public void setSessionLocale(Locale locale) {
        this.locale = locale;
    }

    public Locale getSessionLocale() {
        return this.locale;
    }

    public String getSessionLocaleName() {
        return STRUTS_LOCALE_KEY;
    }

    protected void resetSessionLocale(HttpServletRequest request) {

        HttpSession session = request.getSession();
        if (session!=null) {

            setSessionLocale((Locale)
                session.getAttribute(getSessionLocaleName()));

        }
        else {

            setSessionLocale(Locale.getDefault());
        }

    } // end resetSessionLocale


    protected void putSessionLocale(HttpServletRequest request) {

        Locale locale = getSessionLocale();
        if (null==locale) locale = Locale.getDefault();

        request.getSession(true).setAttribute(Action.LOCALE_KEY,locale);

    } // end putSessionLocale


    public String getLocaleDisplay() {

         Locale locale = getSessionLocale();
         if (null==locale) locale = Locale.getDefault();
         return locale.getDisplayName();

     } // end getLocaleDisplay


     public void setLocaleDisplay(String language) {
         setSessionLocale(new Locale(language,EMPTY));
     }

These and other convenience methods are part of the BaseAction and BaseForm in the Scaffold package of the Struts contrib folder.

HTH, Ted.
---
Struts in Action

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact