1 12 package org.displaytag.localization; 13 14 import java.util.Locale ; 15 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.jsp.PageContext ; 18 import javax.servlet.jsp.tagext.Tag ; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.displaytag.Messages; 23 import org.springframework.context.MessageSource; 24 import org.springframework.web.servlet.support.RequestContextUtils; 25 26 27 33 public class I18nSpringAdapter implements LocaleResolver, I18nResourceProvider 34 { 35 36 39 public static final String UNDEFINED_KEY = "???"; 41 44 private static Log log = LogFactory.getLog(I18nSpringAdapter.class); 45 46 49 public Locale resolveLocale(HttpServletRequest request) 50 { 51 return RequestContextUtils.getLocale(request); 52 } 53 54 57 public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext) 58 { 59 MessageSource messageSource = RequestContextUtils.getWebApplicationContext(pageContext.getRequest()); 60 if (messageSource == null) 61 { 62 log.warn("messageSource not found"); 63 return null; 64 } 65 66 String key = (resourceKey != null) ? resourceKey : defaultValue; 68 69 String message = null; 70 71 message = messageSource.getMessage(key, null, null, RequestContextUtils 72 .getLocale((HttpServletRequest ) pageContext.getRequest())); 73 74 if (message == null && resourceKey != null) 76 { 77 log.debug(Messages.getString("Localization.missingkey", resourceKey)); message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY; 79 } 80 81 return message; 82 83 } 84 } 85 | Popular Tags |