[stringtemplate-interest] Trouble with lazy evaluation of templates inside of resource files
Terence Parr
parrt at cs.usfca.edu
Mon Dec 12 09:54:18 PST 2011
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
More information about the stringtemplate-interest
mailing list