1 12 package org.displaytag.localization; 13 14 import java.util.Locale ; 15 import java.util.MissingResourceException ; 16 import java.util.ResourceBundle ; 17 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.jsp.PageContext ; 20 import javax.servlet.jsp.jstl.core.Config; 21 import javax.servlet.jsp.jstl.fmt.LocalizationContext; 22 import javax.servlet.jsp.tagext.Tag ; 23 import javax.servlet.jsp.tagext.TagSupport ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.taglibs.standard.tag.common.fmt.BundleSupport; 28 import org.displaytag.Messages; 29 30 31 55 public class I18nJstlAdapter implements I18nResourceProvider, LocaleResolver 56 { 57 58 61 public static final String UNDEFINED_KEY = "???"; 63 66 private static Log log = LogFactory.getLog(I18nJstlAdapter.class); 67 68 71 public I18nJstlAdapter() 72 { 73 BundleSupport.class.hashCode(); 76 } 77 78 81 public Locale resolveLocale(HttpServletRequest request) 82 { 83 Locale locale = (Locale ) Config.get(request.getSession(), Config.FMT_LOCALE); 84 if (locale == null) 85 { 86 locale = request.getLocale(); 87 } 88 return locale; 89 } 90 91 94 public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext) 95 { 96 97 String key = (resourceKey != null) ? resourceKey : defaultValue; 99 String title = null; 100 ResourceBundle bundle = null; 101 102 Tag bundleTag = TagSupport.findAncestorWithClass(tag, BundleSupport.class); 104 if (bundleTag != null) 105 { 106 BundleSupport parent = (BundleSupport) bundleTag; 107 if (key != null) 108 { 109 String prefix = parent.getPrefix(); 110 if (prefix != null) 111 { 112 key = prefix + key; 113 } 114 } 115 bundle = parent.getLocalizationContext().getResourceBundle(); 116 } 117 118 if (bundle == null) 120 { 121 Object cauchoBundle = pageContext.getAttribute("caucho.bundle"); if (cauchoBundle != null && cauchoBundle instanceof LocalizationContext) 123 { 124 bundle = ((LocalizationContext) cauchoBundle).getResourceBundle(); 125 126 String prefix = (String ) pageContext.getAttribute("caucho.bundle.prefix"); if (prefix != null) 129 { 130 key = prefix + key; 131 } 132 } 133 } 134 135 if (bundle == null) 137 { 138 LocalizationContext localization = BundleSupport.getLocalizationContext(pageContext); 140 141 if (localization != null) 142 { 143 bundle = localization.getResourceBundle(); 144 } 145 } 146 147 if (bundle != null) 148 { 149 try 150 { 151 title = bundle.getString(key); 152 } 153 catch (MissingResourceException e) 154 { 155 log.debug(Messages.getString("Localization.missingkey", key)); 157 if (resourceKey != null) 159 { 160 title = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY; 161 } 162 } 163 } 164 165 return title; 166 } 167 } 168 | Popular Tags |