1 23 24 package org.apache.slide.util; 25 26 import java.text.*; 27 import java.util.*; 28 import org.apache.slide.common.Domain; 29 30 35 public class Messages { 36 37 38 40 41 44 public static final String ResourceName = 45 "org.apache.slide.util.resources.messages"; 46 47 48 50 51 54 private static ResourceBundle _messages; 55 56 57 60 private static Hashtable _formats; 61 62 63 65 66 72 public static String format(String message, Object arg1) { 73 return format( message, new Object [] { arg1 } ); 74 } 75 76 77 84 public static String format(String message, Object arg1, Object arg2) { 85 return format( message, new Object [] { arg1, arg2 } ); 86 } 87 88 89 97 public static String format(String message, Object arg1, Object arg2, 98 Object arg3) { 99 return format( message, new Object [] { arg1, arg2, arg3 } ); 100 } 101 102 103 109 public static String format( String message, Object [] args ) 110 { 111 MessageFormat mf; 112 String msg; 113 114 try { 115 mf = (MessageFormat) _formats.get( message ); 116 if (mf == null) { 117 try { 118 msg = _messages.getString( message ); 119 } catch (MissingResourceException except) { 120 return message; 121 } 122 mf = new MessageFormat(msg); 123 _formats.put(message, mf); 124 } 125 return mf.format(args); 126 } catch (Exception except) { 127 return "An internal error occured while processing message " 128 + message; 129 } 130 } 131 132 133 138 public static String message(String message) { 139 try { 140 return _messages.getString(message); 141 } catch (MissingResourceException except) { 142 return message; 143 } 144 } 145 146 147 152 public static void setLocale(Locale locale) { 153 _formats = new Hashtable(); 154 try { 155 if (locale == null) { 156 _messages = ResourceBundle.getBundle(ResourceName); 157 } else { 158 _messages = ResourceBundle.getBundle(ResourceName, locale); 159 } 160 } catch (Exception except) { 161 _messages = new EmptyResourceBundle(); 162 Domain.error("Failed to locate messages resource " + ResourceName); 163 } 164 } 165 166 167 169 170 static { 171 setLocale(Locale.getDefault()); 172 } 173 174 175 177 178 static class EmptyResourceBundle 179 extends ResourceBundle 180 implements Enumeration { 181 182 public Enumeration getKeys() { 183 return this; 184 } 185 186 protected Object handleGetObject(String name) { 187 return "[Missing message " + name + "]"; 188 } 189 190 public boolean hasMoreElements() { 191 return false; 192 } 193 194 public Object nextElement() { 195 return null; 196 } 197 198 } 199 200 } 201 | Popular Tags |