cancel
Showing results for 
Search instead for 
Did you mean: 

Change locale in application from Web client

chozero
Champ in-the-making
Champ in-the-making
Hello,

I am developing a Web Site based on Surf and I would like it to have I18N, allowing the user to choose between some languages in the web client. Where should I look? Is there any Web Script or something in the API where I can change the app locale for the session? I really don't know where to start.

Thanks in advance.
1 REPLY 1

chozero
Champ in-the-making
Champ in-the-making
Hi again,

I'll tell how I'm doing this now because it may be helpful for someone else. To achieve this, I modified the way the DispatcherServlet sets locale. Currently, it reads the browser locale settings from request header. I modified this so it reads from a session attribute ("locale") and to change this session attribute I pass a parameter in the URI ("lang=en" or "lang=es", so on). Maybe it would be better changing locale via Web Script.

Code:

    protected void setLanguage(HttpServletRequest request) {
       String lang = request.getParameter(LANG);
       if (lang != null) {
          request.getSession().setAttribute(LOCALE, I18NUtil.parseLocale(lang));
       }
        Locale locale = (Locale)request.getSession().getAttribute(LOCALE);
        if (locale == null) {
           setLanguageFromRequestHeader(request);
        } else {
           I18NUtil.setLocale(locale);
        }
    }