1 29 30 package echo2example.email; 31 32 import java.text.DateFormat ; 33 import java.text.MessageFormat ; 34 import java.util.Date ; 35 import java.util.Locale ; 36 import java.util.HashMap ; 37 import java.util.Map ; 38 import java.util.MissingResourceException ; 39 import java.util.ResourceBundle ; 40 41 import nextapp.echo2.app.ApplicationInstance; 42 43 46 public class Messages { 47 48 private static final String BUNDLE_NAME = "echo2example.email.resource.localization.Messages"; 49 50 54 private static final Map DATE_FORMAT_MEDIUM_MAP = new HashMap (); 55 56 62 public static final String formatDateTimeMedium(Date date) { 63 Locale locale = ApplicationInstance.getActive().getLocale(); 64 DateFormat df = (DateFormat ) DATE_FORMAT_MEDIUM_MAP.get(locale); 65 if (df == null) { 66 df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale); 67 DATE_FORMAT_MEDIUM_MAP.put(locale, df); 68 } 69 return date == null ? null : df.format(date); 70 } 71 72 79 public static String getFormattedString(String key, Object [] arguments) { 80 Locale locale = ApplicationInstance.getActive().getLocale(); 81 String template = getString(key); 82 MessageFormat messageFormat = new MessageFormat (template); 83 messageFormat.setLocale(locale); 84 return messageFormat.format(arguments, new StringBuffer (), null).toString(); 85 } 86 87 94 public static String getString(String key) { 95 try { 96 Locale locale = ApplicationInstance.getActive().getLocale(); 97 ResourceBundle resource = ResourceBundle.getBundle(BUNDLE_NAME, locale); 98 return resource.getString(key); 99 } catch (MissingResourceException e) { 100 return '!' + key + '!'; 101 } 102 } 103 104 105 private Messages() { } 106 } 107 | Popular Tags |