6

I have an Action class, and I want to get the locale of my app and change it here, but I don't know how and can't find an answer.

I can get the current locale using super.getLocale().toString();

But how to set locale I don't know.

public class LoginAction extends ActionSupport {

    private String login;
    private String password;
    private String language;

    @Override
    public String execute() throws Exception {
        String result = Factory.INSTANCE.getUserDao().checkUser(login, password);
        if(result == null){
            return ERROR;
        }
        return SUCCESS;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    @Override
    public void validate() {
        super.validate();
        if(login.isEmpty() | password.isEmpty()){
            addActionError(getText("login.error"));
        }else {
            addActionMessage(getText("login.correct"));
        }
    }
}


I want to change the locale for my app right in the above Action class. How can I do this?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75
  • Try to see this one: http://struts.apache.org/2.1.8/docs/i18n-interceptor.html. It says "attributeName (optional) - the name of the session key to store the selected locale. By default this is WW_TRANS_I18N_LOCALE". It seems that it should be fine to set this session attribute to your new locale. (I've never used Struts2 though) – Peter Štibraný Oct 15 '12 at 08:11

1 Answers1

10

Use ActionContext.getContext().setLocale(locale) and to put it in HTTP session session.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, locale).

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143