Всем привет, возникла следующая ситуация - Есть страничка, есть бин локализации, есть бандлы, всё прекрасно работает, но когда обновляешь страницу, локаль сбрасывается по умолчанию... Итак: faces-config.xml | Код | <?xml version="1.0" encoding="UTF-8"?> <!-- This file is not required if you don't need any extra configuration. --> <faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> <application>
<resource-bundle> <base-name>i18n</base-name> <var>i18n</var> </resource-bundle>
<message-bundle> messages </message-bundle>
<locale-config> <!--<default-locale>en_US</default-locale>--> <default-locale>en_US</default-locale> <supported-locale>ru_RU</supported-locale> </locale-config>
</application> </faces-config> |
Сама страничка с полем для выбора: | Код | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
> <body> <ui:composition>
<div class="site-footer"> <h:form> <h3><h:outputText value="#{i18n['header.description.bottom']}"/></h3> <h:panelGrid columns="2"> Language : <h:selectOneMenu value="#{localizationBean.locale}" onchange="submit()" valueChangeListener="#{localizationBean.localeChanged}"> <f:selectItems value="#{localizationBean.languages}"/> </h:selectOneMenu> <h:outputText value="#{localizationBean.locale}"/> </h:panelGrid> </h:form> </div>
</ui:composition> </body> </html> |
или так можно: | Код | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
> <f:view locale="#{localizationBean.locale}"> <body>
<ui:composition>
<div class="site-footer"> <h:form> <h3><h:outputText value="#{i18n['header.description.bottom']}"/></h3> <h:panelGrid columns="2"> Language : <h:selectOneMenu value="#{localizationBean.locale}" onchange="submit()" valueChangeListener="#{localizationBean.localeChanged}"> <f:selectItems value="#{localizationBean.languages}"/> </h:selectOneMenu> <h:outputText value="#{localizationBean.locale}"/> </h:panelGrid> </h:form> </div>
</ui:composition> </body> </f:view> </html> |
LocalizationBean | Код | import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; import javax.faces.context.FacesContext; import javax.faces.event.ValueChangeEvent; import javax.inject.Named; import javax.servlet.http.HttpServletRequest; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map;
@Named @SessionScoped public class LocalizationBean implements Serializable {
private static final long serialVersionUID = 1L; private String locale = FacesContext.getCurrentInstance() .getViewRoot().getLocale().toString(); private static Map<String, Locale> languages;
@PostConstruct public void init() { languages = new LinkedHashMap<String, Locale>(); languages.put("English", new Locale("en","US")); languages.put("Russian", new Locale("ru","RU"));
//String value;
// if (locale.equals("en_US")){
// value = "English";
// } else {
// value = "Russian";
// }
// FacesContext.getCurrentInstance() // .getViewRoot().setLocale(languages.get(value)); }
public Map<String, Locale> getLanguages() { return languages; }
public String getLocale() { return locale; }
public void setLocale(String locale) { this.locale = locale; }
//value change event listener public void localeChanged(ValueChangeEvent valueChangeEvent) {
String newLocaleValue = valueChangeEvent.getNewValue().toString(); for (Map.Entry<String, Locale> entry : languages.entrySet()) { if (entry.getValue().toString().equals(newLocaleValue)) { FacesContext.getCurrentInstance() .getViewRoot().setLocale((Locale)entry.getValue()); } }
}
}
|
Вот как то так, всё и работает прекрасно, но при обновлении сбрасывается на английский!
|