Creamos una clase
import java.io.Serializable;
import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.annotation.ManagedBean;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
/**
*
* @author avbravo
*/
@ManagedBean
@SessionScoped
@Named(value = "idiomas")
public class Idiomas implements Serializable {
private static final long serialVersionUID = 1L;
public Idiomas() {
}
private static String locale = Locale.getDefault().getDisplayLanguage();
public void setLocale(String locale) {
this.locale = locale;
}
public synchronized String getLocale() {
return locale;
}
public synchronized String changeLanguage() {
return "changed";
}
public String englishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(Locale.ENGLISH);
this.locale = "en";
return null;
}
public String spanishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(new Locale("es"));
this.locale = "es";
return null;
}
}
En la pagina xhtml, en el head
< f:view locale="#{idiomas.locale}">
y creamos commandLink
< h:commandLink action="#{idiomas.englishAction}" value="#{mensajes['idioma.english']}" />
< h:commandLink action="#{idiomas.spanishAction}" value="#{mensajes['idioma.spanish']}" />
import java.io.Serializable;
import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.annotation.ManagedBean;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
/**
*
* @author avbravo
*/
@ManagedBean
@SessionScoped
@Named(value = "idiomas")
public class Idiomas implements Serializable {
private static final long serialVersionUID = 1L;
public Idiomas() {
}
private static String locale = Locale.getDefault().getDisplayLanguage();
public void setLocale(String locale) {
this.locale = locale;
}
public synchronized String getLocale() {
return locale;
}
public synchronized String changeLanguage() {
return "changed";
}
public String englishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(Locale.ENGLISH);
this.locale = "en";
return null;
}
public String spanishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(new Locale("es"));
this.locale = "es";
return null;
}
}
En la pagina xhtml, en el head
< f:view locale="#{idiomas.locale}">
y creamos commandLink
< h:commandLink action="#{idiomas.englishAction}" value="#{mensajes['idioma.english']}" />
< h:commandLink action="#{idiomas.spanishAction}" value="#{mensajes['idioma.spanish']}" />
Comments