如何封装参数化消息的逻辑?

时间:2012-05-09 08:43:04

标签: java java-ee properties jstl message

我正在使用java.util.resourcebundle格式化我的JSTL消息,这很好用:

我使用你可以在这里看到的MessageFormat类。现在我想将它封装到一个只有getParametrizedMessage(String key, String[]parameters)的方法,但我不知道该怎么做。现在有很多工作要显示一个或两个带参数的消息:

UserMessage um = null;   
ResourceBundle messages = ResourceBundle.getBundle("messages");
String str = messages.getString("PF1");
Object[] messageArguments = new String[]{nyreg.getNummer()};
MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(messages.getString("PI14"));
String outputPI14 = formatter.format(messageArguments);
formatter.applyPattern(messages.getString("PI15"));
String outputPI15 = formatter.format(messageArguments)
if(ipeaSisFlag) 
if(checkIfPCTExistInDB && nyreg.isExistInDB()) {            
//um = new ExtendedUserMessage(MessageHandler.getParameterizedMessage("PI15", new String[]{nyreg.getNummer()}) , UserMessage.TYPE_INFORMATION, "Info");
um = new ExtendedUserMessage(outputPI15 , UserMessage.TYPE_INFORMATION, "Info");

......等等。现在我可以将这个逻辑移动到一个静态类MessageHandler.getParameterizedMessage,它现在不工作,看起来像这样:

private final static String dictionaryFileName="messages.properties";

public static String getParameterizedMessage(String key, String [] params){
        if (dictionary==null){
            loadDictionary();
        }
        return getParameterizedMessage(dictionary,key,params);
    }

    private static void loadDictionary(){       
        String fileName = dictionaryFileName;   
                try {
            dictionary=new Properties();
            InputStream fileInput = MessageHandler.class.getClassLoader().getResourceAsStream(fileName);
            dictionary.load(fileInput);
            fileInput.close();
        }
        catch(Exception e) {
            System.err.println("Exception reading propertiesfile in init "+e);
            e.printStackTrace();
            dictionary=null;
        }
    }

如何使用我的参数化消息像使用键和参数调用方法一样简单?

感谢您的帮助

更新

逻辑来自一个继承的方法,在这个扩展的抽象类中。该方法如下:

    protected static String getParameterizedMessage(Properties dictionary,String key,String []params){
        if (dictionary==null){
            return "ERROR";
        }
        String msg = dictionary.getProperty(key);
        if (msg==null){
            return "?!Meddelande " +key + " saknas!?";
        }
        if (params==null){
            return msg;
        }
        StringBuffer buff = new StringBuffer(msg);
        for (int i=0;i<params.length;i++){
            String placeHolder = "<<"+(i+1)+">>";
            if (buff.indexOf(placeHolder)!=-1){
                replace(buff,placeHolder,params[i]);
            }
            else {
                remove(buff,placeHolder);
            }
        }
        return buff.toString();
    }

我认为我必须重写上面的方法,以使其像资源捆绑一样工作,而不仅仅是字典。

更新2

似乎有效的代码在这里

public static String getParameterizedMessage(String key, Object [] params){

    ResourceBundle messages = ResourceBundle.getBundle("messages");
    MessageFormat formatter = new MessageFormat("");
    formatter.applyPattern(messages.getString(key));
    return formatter.format(params);
}

1 个答案:

答案 0 :(得分:1)

我不确定你要做什么,这就是我过去所做的:

public static final String localize(final Locale locale, final String key, final Object... param) {
    final String name = "message";
    final ResourceBundle rb;

    /* Resource bundles are cached internally,
       never saw a need to implement another caching level
     */
    try {
        rb = ResourceBundle.getBundle(name, locale, Thread.currentThread()
                .getContextClassLoader());
    } catch (MissingResourceException e) {
        throw new RuntimeException("Bundle not found:" + name);
    }

    String keyValue = null;

    try {
        keyValue = rb.getString(key);
    } catch (MissingResourceException e) {
        // LOG.severe("Key not found: " + key);
        keyValue = "???" + key + "???";
    }

    /* Message formating is expensive, try to avoid it */
    if (param != null && param.length > 0) {
        return MessageFormat.format(keyValue, param);
    } else {
        return keyValue;
    }
}