Util
Last updated
Was this helpful?
Last updated
Was this helpful?
Dentro del paquete util
Crear las clases Idiomas.java, ResourcesFiles.java
Indicamos el soporte para varios lenguajes.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.avbravo.spard.util;
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;
}
}
En el método saveLocale() indicar la ruta del archivo properties.,
public void saveLocale() {
currentLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
mrb = ResourceBundle.getBundle("com.avbravo.spard.properties.messages",
currentLocale);
arb = ResourceBundle.getBundle("com.avbravoutils.properties.application",
currentLocale);
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.avbravo.spard.util;
import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
/**
*
* @authoravbravo
*/
@Named
@SessionScoped
public class ResourcesFiles implements Serializable {
private static final long serialVersionUID = 1L;
Locale currentLocale;
ResourceBundle mrb; //for messages atributos
ResourceBundle arb; //for application
public ResourcesFiles() {
}
@PostConstruct
public void init() {
saveLocale();
}
public void saveLocale() {
currentLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
mrb = ResourceBundle.getBundle("com.avbravo.spard.properties.messages",
currentLocale);
arb = ResourceBundle.getBundle("com.avbravoutils.properties.application",
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;
}
public ResourceBundle getArb() {
return arb;
}
public void setArb(ResourceBundle arb) {
this.arb = arb;
}
/*
*Devuelve el mensaje Mrb
*/
public String getMessage(String mensaje) {
return mrb.getString(mensaje);
}
/*
*Devuelve el mensaje Arb
*/
public String getAppMessage(String mensaje) {
return arb.getString(mensaje);
}
}