1 16 17 package javax.servlet.jsp.jstl.fmt; 18 19 import java.text.MessageFormat ; 20 import java.util.MissingResourceException ; 21 import java.util.ResourceBundle ; 22 23 import javax.servlet.jsp.PageContext ; 24 25 import org.apache.taglibs.standard.tag.common.fmt.BundleSupport; 26 import org.apache.taglibs.standard.tag.common.fmt.MessageSupport; 27 28 39 40 public class LocaleSupport { 41 42 61 public static String getLocalizedMessage(PageContext pageContext, 62 String key) { 63 return getLocalizedMessage(pageContext, key, null, null); 64 } 65 66 83 public static String getLocalizedMessage(PageContext pageContext, 84 String key, 85 String basename) { 86 return getLocalizedMessage(pageContext, key, null, basename); 87 } 88 89 107 public static String getLocalizedMessage(PageContext pageContext, 108 String key, 109 Object [] args) { 110 return getLocalizedMessage(pageContext, key, args, null); 111 } 112 113 132 public static String getLocalizedMessage(PageContext pageContext, 133 String key, 134 Object [] args, 135 String basename) { 136 LocalizationContext locCtxt = null; 137 String message = MessageSupport.UNDEFINED_KEY + key 138 + MessageSupport.UNDEFINED_KEY; 139 140 if (basename != null) { 141 locCtxt = BundleSupport.getLocalizationContext(pageContext, basename); 142 } else { 143 locCtxt = BundleSupport.getLocalizationContext(pageContext); 144 } 145 146 if (locCtxt != null) { 147 ResourceBundle bundle = locCtxt.getResourceBundle(); 148 if (bundle != null) { 149 try { 150 message = bundle.getString(key); 151 if (args != null) { 152 MessageFormat formatter = new MessageFormat (""); 153 if (locCtxt.getLocale() != null) { 154 formatter.setLocale(locCtxt.getLocale()); 155 } 156 formatter.applyPattern(message); 157 message = formatter.format(args); 158 } 159 } catch (MissingResourceException mre) { 160 } 161 } 162 } 163 164 return message; 165 } 166 } 167 168 | Popular Tags |