Fix so widgets can invoke I18N method.

This commit is contained in:
j2blake 2013-05-20 13:31:25 -04:00
parent 9a7a385b87
commit dbcc75dc20

View file

@ -10,6 +10,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -107,6 +108,7 @@ public abstract class Widget {
private String processMacroToString(Environment env, String widgetName, Macro macro, Map<String, Object> map) { private String processMacroToString(Environment env, String widgetName, Macro macro, Map<String, Object> map) {
StringWriter out = new StringWriter(); StringWriter out = new StringWriter();
try { try {
String templateString = macro.getChildNodes().get(0).toString(); String templateString = macro.getChildNodes().get(0).toString();
// NB Using this method of creating a template from a string does not allow the widget template to import // NB Using this method of creating a template from a string does not allow the widget template to import
@ -118,7 +120,15 @@ public abstract class Widget {
// We need to give each widget macro template a unique key in the StringTemplateLoader, and check // We need to give each widget macro template a unique key in the StringTemplateLoader, and check
// if it's already there or else add it. Leave this for later. // if it's already there or else add it. Leave this for later.
Template template = new Template("widget", new StringReader(templateString), env.getConfiguration()); Template template = new Template("widget", new StringReader(templateString), env.getConfiguration());
template.process(map, out);
// JB KLUGE The widget is processed in its own environment, which doesn't include these custom attributes.
// JB KLUGE Put them in.
Environment widgetEnv = template.createProcessingEnvironment(map, out);
ServletRequest request = (ServletRequest) env.getCustomAttribute("request");
widgetEnv.setCustomAttribute("request", request);
widgetEnv.setCustomAttribute("context", env.getCustomAttribute("context"));
widgetEnv.setLocale(request.getLocale());
widgetEnv.process();
} catch (Exception e) { } catch (Exception e) {
log.error("Could not process widget " + widgetName, e); log.error("Could not process widget " + widgetName, e);
} }