1 12 package org.displaytag.localization; 13 14 import java.util.Locale ; 15 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpSession ; 18 import javax.servlet.jsp.PageContext ; 19 import javax.servlet.jsp.tagext.Tag ; 20 21 import org.apache.struts.Globals; 22 import org.apache.struts.config.ModuleConfig; 23 import org.apache.struts.util.MessageResources; 24 25 26 32 public class I18nStrutsAdapter implements I18nResourceProvider, LocaleResolver 33 { 34 35 38 public static final String UNDEFINED_KEY = "???"; 40 43 public Locale resolveLocale(HttpServletRequest request) 44 { 45 Locale userLocale = null; 46 HttpSession session = request.getSession(false); 47 48 if (session != null) 50 { 51 userLocale = (Locale ) session.getAttribute(Globals.LOCALE_KEY); 52 } 53 54 if (userLocale == null) 55 { 56 userLocale = request.getLocale(); 58 } 59 60 return userLocale; 61 } 62 63 66 public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext) 67 { 68 69 String key = (resourceKey != null) ? resourceKey : defaultValue; 71 72 MessageResources resources = (MessageResources) pageContext.getAttribute( 74 Globals.MESSAGES_KEY, 75 PageContext.REQUEST_SCOPE); 76 77 if (resources == null) 78 { 79 ModuleConfig moduleConfig = (ModuleConfig) pageContext.getRequest().getAttribute(Globals.MODULE_KEY); 80 81 if (moduleConfig == null) 82 { 83 moduleConfig = (ModuleConfig) pageContext.getServletContext().getAttribute(Globals.MODULE_KEY); 84 pageContext.getRequest().setAttribute(Globals.MODULE_KEY, moduleConfig); 85 } 86 87 resources = (MessageResources) pageContext.getAttribute( 88 Globals.MESSAGES_KEY + moduleConfig.getPrefix(), 89 PageContext.APPLICATION_SCOPE); 90 } 91 92 if (resources == null) 93 { 94 resources = (MessageResources) pageContext 95 .getAttribute(Globals.MESSAGES_KEY, PageContext.APPLICATION_SCOPE); 96 } 97 98 String title = null; 99 if (resources != null) 100 { 101 Locale userLocale = resolveLocale((HttpServletRequest ) pageContext.getRequest()); 102 title = resources.getMessage(userLocale, key); 103 } 104 105 if (title == null && resourceKey != null) 107 { 108 title = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY; 109 } 110 111 return title; 112 } 113 114 } 115 | Popular Tags |