我正在尝试使用JSF框架练习国际化流程,但是当我在InputText字段中输入en
时,我在浏览器中更改语言时遇到问题,因此浏览器不会更改语言。 / p>
如何在en
inputText
时更改浏览器语言
的index.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view locale="#{sessionBean.locale}">
<head>
<meta charset="ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<H1>#{locale['welcome']}</H1>
<BR />
<BR />
<H2>#{locale['login.or.register']}</H2>
<h:form>
<h:inputText value="#{sessionBean.lang}" required="true"/>
<br/>
<h:commandButton value="Change" action="#{sessionBean.set()}"/>
<h:commandLink value="Log out" action="#{sessionBean.logout()}"/>
</h:form>
</body>
</f:view>
</html>
的SessionBean
package com.beans;
import java.io.Serializable;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
@ManagedBean
@SessionScoped
public class SessionBean implements Serializable {
private String lang;
private Locale locale;
@PostConstruct
public void init() {
locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
String initLang = locale.getLanguage();
System.out.println("SessionBean - init() - initLang: " + initLang);
}
public String set() {
locale = new Locale(lang);
String language1 = locale.getLanguage();
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
String language2 = FacesContext.getCurrentInstance().getViewRoot().getLocale().getLanguage();
// Locale locale2 =FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
// String initLang2 = locale2.getLanguage();
System.out.println("SessionBean - set(): " + "language1: " + language1 + " ,language2: " + language2);
return "/index?faces-redirect=true";
}
public Locale getLocale() {
System.out.println("SessionBean - getLocaleBean()");
return locale;
}
public String logout() {
HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
ses.invalidate();
System.out.println("SessionBean - logout()");
return "/index?faces-redirect=true";
}
public String getLang() {
System.out.println("SessionBean - getLang()");
return lang;
}
public void setLang(String lang) {
this.lang = lang;
System.out.println("SessionBean - setLang()");
}
}
SessionBean类的输出
2016-07-14T22:43:18.054+0200|Information: SessionBean - init() - initLang: de
2016-07-14T22:43:18.054+0200|Information: SessionBean - getLocaleBean()
2016-07-14T22:43:18.057+0200|Information: SessionBean - getLocaleBean()
2016-07-14T22:43:18.071+0200|Information: SessionBean - getLang()
2016-07-14T22:43:18.073+0200|Information: SessionBean - setLang()
2016-07-14T22:44:05.105+0200|Information: SessionBean - set(): language1: en ,language2: en
2016-07-14T22:44:43.181+0200|Information: SessionBean - init() - initLang: de
2016-07-14T22:44:43.181+0200|Information: SessionBean - getLocaleBean()
2016-07-14T22:44:43.184+0200|Information: SessionBean - getLocaleBean()
2016-07-14T22:44:43.191+0200|Information: SessionBean - getLang()
面-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>de</supported-locale>
</locale-config>
<resource-bundle>
<base-name>
locales.locale
</base-name>
<var>
locale
</var>
</resource-bundle>
</application>
</faces-config>
locale_en.properties
welcome = Welcome to MyApp.com
login.or.register = Please login or register
locale_de.properties
welcome = Herzlich Willkommen zu MyApp.com
login.or.register = Bitte Einlogen oder Registerieren