1 25 26 package com.j2biz.blogunity.i18n; 27 28 import java.text.MessageFormat ; 29 import java.util.Locale ; 30 import java.util.ResourceBundle ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 34 import org.apache.commons.lang.StringUtils; 35 36 import com.j2biz.blogunity.BlogunityManager; 37 import com.j2biz.blogunity.IConstants; 38 39 public class I18NMessageManager { 40 41 private static final String ERRORS_BUNDLE_NAME = "i18n_errors"; 42 43 private static final String MESSAGES_BUNDLE_NAME = "i18n_messages"; 44 45 private static final String NAVIGATION_BUNDLE_NAME = "i18n_navigation"; 46 47 private static I18NMessageManager INSTANCE = null; 48 49 private I18NMessageManager() { 50 51 } 52 53 public static I18NMessageManager getInstance() { 54 if (INSTANCE == null) INSTANCE = new I18NMessageManager(); 55 return INSTANCE; 56 } 57 58 61 private Locale getSystemLocale() { 62 return BlogunityManager.getSystemConfiguration().getSystemLocale(); 63 } 64 65 69 public String getMessage(String key) { 70 return getMessage(key, new String []{}, getSystemLocale()); 71 } 72 73 78 public String getMessage(String key, String [] params) { 79 return getMessage(key, params, getSystemLocale()); 80 } 81 82 87 public String getMessage(String key, Locale locale) { 88 return getMessage(key, new String []{}, locale); 89 } 90 91 public String getMessage(String key, HttpServletRequest request) { 92 return getMessage(key, new String []{}, request); 93 } 94 95 public String getMessage(String key, String [] params, HttpServletRequest request) { 96 97 Locale locale = (Locale ) request.getAttribute(IConstants.Request.LOCALE); 98 return getMessage(key, params, locale); 99 } 100 101 107 public String getMessage(String key, String [] params, Locale locale) { 108 return getTextFromBundle(key, params, locale, MESSAGES_BUNDLE_NAME); 109 } 110 111 115 public String getError(String key) { 116 return getError(key, new String []{}, getSystemLocale()); 117 } 118 119 124 public String getError(String key, String [] params) { 125 return getError(key, params, getSystemLocale()); 126 } 127 128 133 public String getError(String key, Locale locale) { 134 return getError(key, new String []{}, locale); 135 } 136 137 143 public String getError(String key, String [] params, Locale locale) { 144 return getTextFromBundle(key, params, locale, ERRORS_BUNDLE_NAME); 145 } 146 147 151 public String getNavigationText(String key) { 152 return getNavigationText(key, new String []{}, getSystemLocale()); 153 } 154 155 160 public String getNavigationText(String key, String [] params) { 161 return getNavigationText(key, params, getSystemLocale()); 162 } 163 164 169 public String getNavigationText(String key, Locale locale) { 170 return getNavigationText(key, new String []{}, locale); 171 } 172 173 179 public String getNavigationText(String key, String [] params, Locale locale) { 180 return getTextFromBundle(key, params, locale, NAVIGATION_BUNDLE_NAME); 181 } 182 183 private String getTextFromBundle(String key, String [] params, Locale locale, String bundleName) { 184 ResourceBundle messagesBundle = ResourceBundle.getBundle(bundleName, locale); 185 186 if (StringUtils.isNotEmpty(key)) { 187 188 String value = null; 189 try { 190 value = new String (messagesBundle.getString(key).getBytes("ISO-8859-1"), "UTF-8"); 191 } catch (Throwable t) { 192 return key; 193 } 194 if (params != null && params.length > 0) { 195 value = MessageFormat.format(value, params); 196 } 197 198 if (StringUtils.isEmpty(value)) return key; 199 else 200 return value; 201 } 202 return I18N.MESSAGES.UNKNOWN; 203 } 204 205 } | Popular Tags |