1 13 package info.magnolia.cms.i18n; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.ItemType; 19 import info.magnolia.cms.core.NodeData; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.Locale ; 25 26 import javax.jcr.RepositoryException; 27 import javax.jcr.observation.Event; 28 import javax.jcr.observation.EventIterator; 29 import javax.jcr.observation.EventListener; 30 import javax.jcr.observation.ObservationManager; 31 import javax.servlet.ServletContext ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpSession ; 34 import javax.servlet.jsp.PageContext ; 35 import javax.servlet.jsp.jstl.core.Config; 36 37 import org.apache.commons.lang.StringUtils; 38 import org.apache.log4j.Logger; 39 40 41 46 public final class MessagesManager { 47 48 51 public static final String FALLBACK_LOCALE = "en"; 53 56 public static final String DEFAULT_BASENAME = "info.magnolia.module.admininterface.messages"; 58 61 protected static Logger log = Logger.getLogger(Messages.class); 62 63 66 private static final String I18N_CONFIG_NAME = "i18n"; 68 71 private static final String LOCALE_CONFIG_NAME = "language"; 73 76 private static final String AVAILABLE_LOCALES_CONFIG_NAME = "availableLanguages"; 78 81 private static Locale applicationLocale; 82 83 86 private static Collection availableLocales = new ArrayList (); 87 88 91 private static ServletContext context; 92 93 96 private MessagesManager() { 97 } 98 99 103 public static void init(ServletContext context) { 104 MessagesManager.context = context; 105 106 context.setAttribute(Config.FMT_FALLBACK_LOCALE + ".application", FALLBACK_LOCALE); context.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".application", MessagesManager.DEFAULT_BASENAME); context.setAttribute(Config.FMT_LOCALIZATION_CONTEXT, MessagesManager.DEFAULT_BASENAME); 112 113 MessagesManager.setDefaultLocale(FALLBACK_LOCALE); 115 116 load(); 117 registerEventListener(); 118 } 119 120 123 public static void load() { 124 125 HierarchyManager configHierarchyManager = ContentRepository.getHierarchyManager(ContentRepository.CONFIG); 127 try { 128 log.info("Config : loading i18n configuration - " + I18N_CONFIG_NAME); 130 Content serverNode = configHierarchyManager.getContent("/server"); 132 Content configNode; 133 try { 134 configNode = serverNode.getContent(I18N_CONFIG_NAME); 135 } 136 catch (javax.jcr.PathNotFoundException e) { 137 configNode = serverNode.createContent(I18N_CONFIG_NAME, ItemType.CONTENTNODE); 138 configHierarchyManager.save(); 139 } 140 141 NodeData languageNodeData = configNode.getNodeData(LOCALE_CONFIG_NAME); 142 143 if (StringUtils.isEmpty(languageNodeData.getName())) { 144 languageNodeData = configNode.createNodeData(LOCALE_CONFIG_NAME); 145 languageNodeData.setValue(MessagesManager.FALLBACK_LOCALE); 146 configHierarchyManager.save(); 147 } 148 149 MessagesManager.setDefaultLocale(languageNodeData.getString()); 150 151 Content availableLanguagesContentNode; 153 154 NodeData availableLanguage; 155 156 try { 157 availableLanguagesContentNode = configNode.getContent(AVAILABLE_LOCALES_CONFIG_NAME); 158 } 159 catch (javax.jcr.PathNotFoundException e) { 160 availableLanguagesContentNode = configNode.createContent( 161 AVAILABLE_LOCALES_CONFIG_NAME, 162 ItemType.CONTENTNODE); 163 164 availableLanguage = availableLanguagesContentNode.createNodeData(MessagesManager.FALLBACK_LOCALE); 165 availableLanguage.setValue(MessagesManager.FALLBACK_LOCALE); 166 configHierarchyManager.save(); 167 } 168 169 Collection locales = availableLanguagesContentNode.getNodeDataCollection(); 170 171 MessagesManager.availableLocales.clear(); 173 174 for (Iterator iter = locales.iterator(); iter.hasNext();) { 175 availableLanguage = (NodeData) iter.next(); 176 String name = availableLanguage.getString(); 177 String language = name; 178 String country = StringUtils.EMPTY; 179 180 if (name.indexOf("_") == 2) { language = name.substring(0, 2); 182 country = name.substring(3); 183 } 184 Locale locale = new Locale (language, country); 185 MessagesManager.availableLocales.add(locale); 186 } 187 188 } 189 catch (RepositoryException re) { 190 log.error("Config : Failed to load i18n configuration - " + I18N_CONFIG_NAME); log.error(re.getMessage(), re); 192 } 193 } 194 195 198 private static void registerEventListener() { 199 200 log.info("Registering event listener for i18n"); 202 try { 203 ObservationManager observationManager = ContentRepository 204 .getHierarchyManager(ContentRepository.CONFIG) 205 .getWorkspace() 206 .getObservationManager(); 207 208 observationManager.addEventListener(new EventListener() { 209 210 public void onEvent(EventIterator iterator) { 211 reload(); 213 } 214 }, Event.NODE_ADDED 215 | Event.NODE_REMOVED 216 | Event.PROPERTY_ADDED 217 | Event.PROPERTY_CHANGED 218 | Event.PROPERTY_REMOVED, "/server/" + I18N_CONFIG_NAME, true, null, null, false); } 220 catch (RepositoryException e) { 221 log.error("Unable to add event listeners for i18n", e); } 223 } 224 225 228 public static void reload() { 229 load(); 230 } 231 232 237 public static Messages getMessages(HttpServletRequest req) { 238 if (req != null) { 239 return new ContextMessages(req); 240 } 241 242 log.debug("using i18n-messages without a request!"); return new Messages(MessagesManager.DEFAULT_BASENAME, applicationLocale); 244 245 } 246 247 253 public static Messages getMessages(HttpServletRequest req, String basename) { 254 if (req != null) { 255 return new ContextMessages(req, basename); 256 } 257 258 log.debug("using i18n-messages without a request!"); return new Messages(basename, applicationLocale); 260 261 } 262 263 270 public static Messages getMessages(HttpServletRequest req, String basename, Locale locale) { 271 if (req != null) { 272 return new ContextMessages(req, basename, locale); 273 } 274 275 log.debug("using i18n-messages without a request!"); return new Messages(basename, locale); 277 278 } 279 280 285 public static Messages getMessages(PageContext pc) { 286 if (pc != null && pc.getRequest() instanceof HttpServletRequest ) { 287 return new ContextMessages((HttpServletRequest ) pc.getRequest()); 288 } 289 290 log.debug("using i18n-messages without a request inside a control!"); return new Messages(MessagesManager.DEFAULT_BASENAME, applicationLocale); 292 293 } 294 295 301 302 public static String get(HttpServletRequest req, String key) { 303 return getMessages(req).get(key); 304 } 305 306 313 314 public static String get(HttpServletRequest req, String key, Object [] args) { 315 return getMessages(req).get(key, args); 316 } 317 318 325 326 public static String getWithDefault(HttpServletRequest req, String key, String defaultMsg) { 327 return getMessages(req).getWithDefault(key, defaultMsg); 328 } 329 330 338 public static String getWithDefault(HttpServletRequest req, String key, Object [] args, String defaultMsg) { 339 return getMessages(req).getWithDefault(key, args, defaultMsg); 340 } 341 342 345 public static Locale getDefaultLocale() { 346 return applicationLocale; 347 } 348 349 352 public static Locale getCurrentLocale(HttpServletRequest req) { 353 try { 354 return ContextMessages.getCurrentLocale(req); 355 } 356 catch (Exception e) { 357 return getDefaultLocale(); 358 } 359 } 360 361 364 public static void setDefaultLocale(String defaultLocale) { 365 MessagesManager.applicationLocale = new Locale (defaultLocale); 366 context.setAttribute(Config.FMT_LOCALE + ".application", defaultLocale); } 368 369 372 public static Collection getAvailableLocales() { 373 return availableLocales; 374 } 375 376 381 public static void setUserLanguage(String language, HttpSession session) { 382 session.setAttribute(Config.FMT_LOCALE + ".session", language); } 384 } | Popular Tags |