JSF set locale per session

IN faces-config.xml:
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>fr</supported-locale>
</locale-config>
</application>


LocaleBean:
package com.example.faces;

import java.util.Locale;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class LocaleBean {

private Locale locale;

@PostConstruct
public void init() {
locale
= FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
}

public Locale getLocale() {
return locale;
}

public String getLanguage() {
return locale.getLanguage();
}

public void setLanguage(String language) {
locale
= new Locale(language);
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}

}
<!DOCTYPE html>
<html lang="#{localeBean.language}"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:view locale="#{localeBean.locale}">
<h:head>
<title>JSF/Facelets i18n example</title>
</h:head>
<h:body>
<h:form>
<h:selectOneMenu value="#{localeBean.language}" onchange="submit()">
<f:selectItem itemValue="en" itemLabel="English" />
<f:selectItem itemValue="fr" itemLabel="French" />
</h:selectOneMenu>
</h:form>
</h:body>
</f:view>
</html>

Comments

Popular posts from this blog

Check MySQL query history from command line

Installing chocolatey on Windows

Fixed: ImportError: No module named utilities.common.tools