gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:15:29 25 4
gpt4 key购买 nike

我正在使用 java.util.resourcebundle 来格式化我的 JSTL 消息,并且效果很好:

我使用 MessageFormat 类,您可以在此处看到。现在我想将其封装到一个方法中,该方法只是 getParametrizedMessage(String key, String[]parameters) 但我不知道该怎么做。现在要显示一两 strip 参数的消息需要做很多工作:

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

最佳答案

我不太确定你想要实现什么目标,这是我过去所做的:

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

关于java - 如何封装参数化消息的逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10512640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com