package com.app; import java.io.Serializable; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.MissingResourceException; import java.util.ResourceBundle; import org.apache.commons.lang.StringUtils; public class MessageUtils implements Serializable{ /** * @param args */ private static String locale; private static ResourceBundle bind; private static String baseName; public static String getBaseName() { return baseName; } public static void setBaseName(String baseName) { MessageUtils.baseName = baseName; bind = null; } public static String getMessage(String key){ String message = null; try { message = getBind().getString(key); } catch (MissingResourceException e) { return "@@ Not found message for key="+key; } if(StringUtils.isBlank(message)) return "@@ Not found message for key="+key; return message; } public static String getMessage(String key,Map<String,String> values){ String message = null; try { message = getBind().getString(key); } catch (MissingResourceException e) { return "@@ Not found message for key="+key; } if(StringUtils.isBlank(message)) return "@@ Not found message for key="+key; if(values==null) return message; Iterator it = values.keySet().iterator(); while(it.hasNext()){ String paraName = it.next().toString(); key.replaceAll("{"+paraName+"}", values.get(paraName).toString()); } return message; } public static String getMessage(String key,String[] values){ String message = null; try { message = getBind().getString(key); } catch (MissingResourceException e) { return "@@ Not found message for key="+key; } if(StringUtils.isBlank(message)) return "@@ Not found message for key="+key; if(values==null||values.length==0) return message; for(int i=0; i<values.length; i++){ key.replaceAll("{"+i+"}", values[i]); } return message; } public static String getMessage(String key,List<String> values){ String message = null; try { message = getBind().getString(key); } catch (MissingResourceException e) { return "@@ Not found message for key="+key; } if(StringUtils.isBlank(message)) return "@@ Not found message for key="+key; if(values==null||values.size()==0) return message; for(int i=0; i<values.size(); i++){ key.replaceAll("{"+i+"}", values.get(i)); } return message; } public String getLocale() { return locale; } public void setLocale(String locale) { this.locale = locale; } private static ResourceBundle getBind() { if(baseName==null) baseName = "locale.message"; if (bind == null) { if (locale == null || locale.equals("")) //src/locale/message.properties bind = ResourceBundle.getBundle(baseName); else bind = ResourceBundle.getBundle(baseName + locale); } return bind; } public void setBind(ResourceBundle bind) { this.bind = bind; } public void reset(){ bind = null; baseName = null; } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(MessageUtils.getMessage("com.s2s3h3.app.testMessage",new String[]{"aaa","bbb"})); MessageUtils.setBaseName("struts"); System.out.println(MessageUtils.getMessage("struts.login.pageName")); } }
MessageUtils:ResourceBundle访问properties文件
一个简单的ResourceBundle 例子
猜你喜欢
转载自panyongzheng.iteye.com/blog/1116896
今日推荐
周排行