Pass parameter in Bundle

Asked

Viewed 366 times

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 ?

1 answer

1


You will use the function String format. for that reason. For example instead of using:

public static void sucesso(String mensagem) {

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, recupera(mensagem), "");
        FacesContext.getCurrentInstance().addMessage("messagePanel", msg);
    }

You will write like this:

public static void sucesso(String mensagem, Object... parametros) {

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, String.format(recupera(mensagem), parametros), "");
        FacesContext.getCurrentInstance().addMessage("messagePanel", msg);
    }

Just one more tip, you are repeating a lot of code in this class, see that the methods success, error and alert do almost the same thing, only changing the level of severity. You could have a private method that these methods would call, plus just the level.

  • How do I pass the parameter in the method call? I did so and it didn’t work: MensagensUtil.sucesso("cadastro.sucesso","geracao");

  • You called correctly, showed some error?

  • 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.

  • He can’t find the value of the key :/

  • Couldn’t find it before? Because the idea is not to change the key search, just add the parameters after.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.