paquete util
Last updated
Was this helpful?
Last updated
Was this helpful?
Este paquete contendrĂ¡ la clase para el soporte de idiomas, y el manejo del archivo properties.
clic derecho New-> New Package
import java.io.Serializable;
import java.util.Locale;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
/**
*
* @authoravbravo
*/
@Named
@SessionScoped
public class Idiomas implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
ResourcesFiles rf;
private 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 Idiomas() {
}
public String englishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(Locale.ENGLISH);
this.locale = "en";
rf.saveLocale();
return null;
}
public String spanishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(new Locale("es"));
this.locale = "es";
rf.saveLocale();
return null;
}
}
Especificamos el paquete donde esta ubicado el archivo message
mrb = ResourceBundle.getBundle("com.javscaz.tallerjsd.properties.messages",
Codigo completo
@Named
@SessionScoped
public class ResourcesFiles implements Serializable {
private static final long serialVersionUID = 1L;
Locale currentLocale;
ResourceBundle mrb; //for messages atributos
public ResourcesFiles() {
}
@PostConstruct
public void init() {
saveLocale();
}
public void saveLocale() {
currentLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
mrb = ResourceBundle.getBundle("com.javscaz.tallerjsd.properties.messages",
currentLocale);
}
public Locale getCurrentLocale() {
return currentLocale;
}
public void setCurrentLocale(Locale currentLocale) {
this.currentLocale = currentLocale;
}
public ResourceBundle getMrb() {
return mrb;
}
public void setMrb(ResourceBundle mrb) {
this.mrb = mrb;
}
/*
*Devuelve el mensaje Mrb
*/
public String getMensaje(String mensaje) {
return mrb.getString(mensaje);
}
}