当我单击mu按钮以在英语和保加利亚语之间更改语言环境时,我遇到了这个问题。当我第二次单击应用程序中的某个位置时,它将其重置为默认语言(保加利亚语)。
我在会话bean中的代码:
private String localeString;
private Locale currentLocale;
public static final Locale BG = new Locale("bg", "BG");
public static final Locale EN = new Locale("en", "US");
//private UserInfo userInfo;
@PostConstruct
public void init() {
currentLocale = initLocale();
localeString = "English";
}
private Locale initLocale() {
if (FacesContext.getCurrentInstance() != null && FacesContext.getCurrentInstance().getApplication() != null) {
FacesContext.getCurrentInstance().getApplication().setDefaultLocale(BG);
}
return BG;
}
public String changeLocale() {
if (currentLocale.equals(BG)) {
FacesContext.getCurrentInstance().getApplication().setDefaultLocale(EN);
currentLocale = EN;
localeString = "Български";
} else {
FacesContext.getCurrentInstance().getApplication().setDefaultLocale(BG);
currentLocale = BG;
localeString = "English";
}
return null;
}
我在Web应用程序标题中的代码:
<h:commandButton action="#{sessionBean.changeLocale}" value="#
{sessionBean.localeString}" styleClass="Btn1" style="font-size: 11px;"/>