1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
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")); } } |