1
My application uses message internationalization, etc. I would like to know how to pass parameters when passing a key that will fetch a value in the file .properties. Ex:
My crud screens will always display a success message when a new registration is made. The default message would be: Produdo cadastrado(a) com sucesso!. The word Product can vary already the rest can be fixed. So in my file . properties is like this:
pruduto=Produto
cadastro.sucesso= {0} cadastrado(a) com sucesso!
Meaning there will always come some value before the key cadastro.sucesso
. Currently my code is like this:
package br.com.pokemax.util;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
public class MensagensUtil {
private final static ResourceBundle BUNDLE = ResourceBundle.getBundle("properties.mensagens", new Locale("pt"));;
public static String recupera(String chave) {
return BUNDLE.getString(chave);
}
public static void sucesso(String mensagem) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, recupera(mensagem), "");
FacesContext.getCurrentInstance().addMessage("messagePanel", msg);
}
public static void erro(String mensagem) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, recupera(mensagem), "");
FacesContext.getCurrentInstance().addMessage("messagePanel", msg);
}
public static void alerta(String mensagem) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, recupera(mensagem), "");
FacesContext.getCurrentInstance().addMessage("messagePanel", msg);
}
}
In this situation I need to pass a key only, etc. How can I accept to pass parameters ?
How do I pass the parameter in the method call? I did so and it didn’t work:
MensagensUtil.sucesso("cadastro.sucesso","geracao");
– Roknauta
You called correctly, showed some error?
– Júlio Neto
In his method he could not find the value of the key, I managed using another way I will post below, even so thank you for your dedication.
– Roknauta
He can’t find the value of the key :/
– Roknauta
Couldn’t find it before? Because the idea is not to change the key search, just add the parameters after.
– Júlio Neto