1 13 package info.magnolia.cms.i18n; 14 15 import java.util.Iterator ; 16 17 import org.apache.commons.lang.StringUtils; 18 19 20 25 public class MessagesUtil { 26 27 31 public static String generateJavaScript(Messages messages) { 32 StringBuffer str = new StringBuffer (); 33 34 str.append("/* ###################################\n"); str.append("### Generated AbstractMessagesImpl\n"); str.append("################################### */\n\n"); 38 for (Iterator iter = messages.keys(); iter.hasNext();) { 39 String key = (String ) iter.next(); 40 41 if (key.endsWith(".js")) { String msg = javaScriptString(messages.get(key)); 43 str.append(AbstractMessagesImpl.JS_OBJECTNAME 44 + ".add('" 45 + key 46 + "','" 47 + msg 48 + "','" 49 + messages.getBasename() 50 + "');"); 51 str.append("\n"); } 53 } 54 return str.toString(); 55 } 56 57 61 public static Messages chain(Messages msgs1, Messages msgs2) { 62 MessagesChain msgs = new MessagesChain(msgs1); 63 msgs.chain(msgs2); 64 return msgs; 65 } 66 67 public static Messages chain(String [] basenames) { 68 Messages head = MessagesManager.getMessages(basenames[0]); 69 MessagesChain chain = new MessagesChain(head); 70 for (int i = 1; i < basenames.length; i++) { 71 String basename = basenames[i]; 72 Messages msgs = MessagesManager.getMessages(basename); 73 chain.chain(msgs); 74 } 75 chain.chain(MessagesManager.getMessages()); 76 return chain; 77 } 78 79 84 public static Messages chain(Messages msgs1, String basename) { 85 Messages msgs2 = MessagesManager.getMessages(basename); 86 return chain(msgs1, msgs2); 87 } 88 89 94 public static Messages chain(String basename, Messages msgs2) { 95 Messages msgs1 = MessagesManager.getMessages(basename); 96 return chain(msgs1, msgs2); 97 } 98 99 103 public static Messages chainWithDefault(String basename) { 104 Messages msgs1 = MessagesManager.getMessages(basename); 105 Messages msgs2 = MessagesManager.getMessages(); 106 return chain(msgs1, msgs2); 107 } 108 109 113 public static String javaScriptString(String msg) { 114 return StringUtils.replace(StringUtils.replace(msg, "'", "\\'"), "\n", "\\n"); 115 } 116 117 public static String get(String key){ 118 return MessagesManager.getMessages().get(key); 119 } 120 121 public static String get(String key,String [] args){ 122 return MessagesManager.getMessages().get(key, args); 123 } 124 125 public static String getWithDefault(String key, String dflt){ 126 return MessagesManager.getMessages().getWithDefault(key, dflt); 127 } 128 129 public static String getWithDefault(String key, String dflt, String [] args){ 130 return MessagesManager.getMessages().getWithDefault(key, args, dflt); 131 } 132 133 public static String get(String key, String basename){ 134 return MessagesManager.getMessages(basename).get(key); 135 } 136 137 public static String get(String key, String basename, String [] args){ 138 return MessagesManager.getMessages(basename).get(key, args); 139 } 140 141 public static String getWithDefault(String key, String dflt, String basename){ 142 return MessagesManager.getMessages(basename).getWithDefault(key, dflt); 143 } 144 145 public static String getWithDefault(String key, String dflt, String basename, String [] args){ 146 return MessagesManager.getMessages(basename).getWithDefault(key, args, dflt); 147 } 148 149 public static String getChained(String key, String [] basenames){ 150 return chain(basenames).get(key); 151 } 152 153 public static String getChained(String key, String [] basenames, String [] args){ 154 return chain(basenames).get(key, args); 155 } 156 157 public static String getChainedWithDefault(String key, String dflt, String [] basenames){ 158 return chain(basenames).getWithDefault(key, dflt); 159 } 160 161 public static String getChainedWithDefault(String key, String dflt, String [] basenames, String [] args){ 162 return chain(basenames).getWithDefault(key, args, dflt); 163 } 164 165 } 166 | Popular Tags |