1 12 package org.displaytag.localization; 13 14 import java.util.Iterator ; 15 import java.util.Locale ; 16 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.jsp.PageContext ; 19 import javax.servlet.jsp.tagext.Tag ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.displaytag.Messages; 24 25 import com.opensymphony.webwork.views.jsp.TagUtils; 26 import com.opensymphony.xwork.ActionContext; 27 import com.opensymphony.xwork.LocaleProvider; 28 import com.opensymphony.xwork.TextProvider; 29 import com.opensymphony.xwork.util.OgnlValueStack; 30 31 32 38 public class I18nWebworkAdapter implements LocaleResolver, I18nResourceProvider 39 { 40 41 44 public static final String UNDEFINED_KEY = "???"; 46 49 private static Log log = LogFactory.getLog(I18nWebworkAdapter.class); 50 51 54 public Locale resolveLocale(HttpServletRequest request) 55 { 56 57 Locale result = null; 58 OgnlValueStack stack = ActionContext.getContext().getValueStack(); 59 60 Iterator iterator = stack.getRoot().iterator(); 61 while (iterator.hasNext()) 62 { 63 Object o = iterator.next(); 64 65 if (o instanceof LocaleProvider) 66 { 67 LocaleProvider lp = (LocaleProvider) o; 68 result = lp.getLocale(); 69 70 break; 71 } 72 } 73 74 if (result == null) 75 { 76 log.debug("Missing LocalProvider actions, init locale to default"); 77 result = Locale.getDefault(); 78 } 79 80 return result; 81 } 82 83 86 public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext) 87 { 88 89 String key = (resourceKey != null) ? resourceKey : defaultValue; 91 92 String message = null; 93 OgnlValueStack stack = TagUtils.getStack(pageContext); 94 Iterator iterator = stack.getRoot().iterator(); 95 96 while (iterator.hasNext()) 97 { 98 Object o = iterator.next(); 99 100 if (o instanceof TextProvider) 101 { 102 TextProvider tp = (TextProvider) o; 103 message = tp.getText(key, null, null); 104 105 break; 106 } 107 } 108 109 if (message == null && resourceKey != null) 111 { 112 log.debug(Messages.getString("Localization.missingkey", resourceKey)); message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY; 114 } 115 116 return message; 117 } 118 } 119 | Popular Tags |