1

我正在使用 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;
        }
    }

我怎样才能使使用我的参数化消息像调用带有键和参数的方法一样简单?

谢谢你的帮助

更新

逻辑来自 this 扩展的抽象类中的继承方法。该方法如下所示:

    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);
}
4

1 回答 1

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;
    }
}
于 2012-05-09T09:23:43.067 回答