如何更改 Struts 操作类中的语言环境?

2022-01-16 00:00:00 locale internationalization java struts2

我有一个 Action 类,我想获取我的应用的语言环境并在此处更改它,但我不知道如何也找不到答案.

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.

我可以使用 super.getLocale().toString();

但是如何设置语言环境我不知道.

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"));
        }
    }
}


我想在上面的 Action 类中更改我的应用程序的语言环境.我该怎么做?


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

推荐答案

使用 ActionContext.getContext().setLocale(locale) 并将其放入 HTTP sessionsession.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, locale).

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

相关文章