[stringtemplate-interest] Trouble with lazy evaluation of templates inside of resource files

George Palmer javaguy4life at hotmail.com
Mon Dec 12 10:34:59 PST 2011


Thanks, but I don't think that is my problem.  
To clarify, I am using StringTemplate v4.0.2 in Java with '$' delimiters.

-George
> Subject: Re: [stringtemplate-interest] Trouble with lazy evaluation of templates inside of resource files
> From: parrt at cs.usfca.edu
> Date: Mon, 12 Dec 2011 09:54:18 -0800
> CC: stringtemplate-interest at antlr.org
> To: javaguy4life at hotmail.com
> 
> hi. if v4, it assumes <…> not $…$.
> Ter
> On Dec 12, 2011, at 9:36 AM, George Palmer wrote:
> 
> > I am using the techniques described in these articles to i18nize my string templates:
> > http://www.stringtemplate.org/article/i18n/index.html
> > http://www.cs.usfca.edu/~parrt/papers/i18n.pdf
> > http://hardlikesoftware.com/weblog/2007/01/15/i18n-with-stringtemplate/
> > 
> > It is my understanding that attributes containing property strings can themselves contain attribute references which will be replaced by virtue of StringTemplate's lazy evaluation.  However, this doesn't seem to be working in my case.
> > 
> > Specifically, I am storing strings in Java ResourceBundles, and I use the STResourceBundleWrapper technique described in that last article to wrap the resource bundle in a map interface.  If the value of the property string contains a delimiter ('$'), then the wrapper returns a ST object.  I've stepped through the code in the debugger and confirmed that STResourceBundleWrapper.get() actually does return return a ST object when ST.render() is called.
> > 
> > Despite the intentions of this wrapper, attributes in my property strings don't get evaluated. The string is rendered literally. 
> > 
> > Below is the code.  Am I misunderstanding how lazy evaluation works with these i18n techniques?  Does anyone see something wrong with the implementation below?  Advice is appreciated.
> > 
> > ===============================
> > MyClass.properties
> > ===============================
> > text="Hello, $username$"
> > 
> > 
> > ===============================
> > MyClass.java
> > ===============================
> > ...
> > ST stemplate = new ST("$message.text$");
> > ResourceBundle bundle = ResourceBundle.getBundle(classPath);
> > 
> > stemplate.setAttribute("message", new STResourceBundleWrapper(bundle));
> > stemplate.setAttribute("username", "User001");
> > 
> > return stemplate.render();
> > ...
> > 
> > 
> > ===============================
> > STResourceBundleWrapper.java
> > ===============================
> > ...
> > public Object get(Object key)
> > {
> > 	...
> > 	Object o = m_wrappedBundle.getObject((String)key);
> > 	...
> > 	return new ST((String)o);
> > 	...
> > }
> > ...
> > 
> > 
> > ===============================
> > Expected Output
> > ===============================
> > Hello, User001
> > 
> > 
> > ===============================
> > Actual Output
> > ===============================
> > Hello, $username$
> > 
> > 
> > 
> > Regards,
> > George Palmer
> > _______________________________________________
> > stringtemplate-interest mailing list
> > stringtemplate-interest at antlr.org
> > http://www.antlr.org/mailman/listinfo/stringtemplate-interest
> 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20111212/d475cedc/attachment.html 


More information about the stringtemplate-interest mailing list