shorthand syntax for wrapping functions?

93 views
Skip to first unread message

Mark Anderson

unread,
Jan 26, 2016, 3:05:05 PM1/26/16
to mustache.java
For i18n and l10n, we'd like to support a more succinct syntax than the way wrapping functions work now.
So instead of:

   {{#translate}}{{salutation}}{{/#translate}}   /* translates "Dear" to "Estimada" for female spanish speaking targets */
   {{#localize}}{{regdate}}{{/#localize}} /* converts "2011-05-17" to "17 de mayo de 2011" */

we'd have just:

  {{translate salutation}}
  {{localize regdate}}

It wouldn't have to be exactly like that -- but you get the idea. For the use case where the content
to be operated on is inside a single variable, this is a lot more succinct.

-mda

Sam

unread,
Feb 3, 2016, 12:23:23 PM2/3/16
to mustache.java
You could definitely implement this by overriding variable resolution but I don't plan on adding non-standard features like this one to the core.Also, I would point out that this strategy for localization / translation will not work in general. I18n is a lot more complicated than simply replacing single words in the vast majority of cases. One of the most complicated extensions to mustache.java at Twitter was the I18n system.

Sam

Mark D. Anderson

unread,
Feb 3, 2016, 1:28:53 PM2/3/16
to mustac...@googlegroups.com
This particular application is generating letters and all the i18n requirements are rather narrow -- generally speaking
just gender matching single words for the person receiving the letter (for gender-matching languages).
 
If I wanted to do this, roughly what would be required? 
I guess that variable lookup doesn't look at internal white space?
So I would need something like an unbound variable handler for "translate salutation" from {{translate salutation}} ?
 
Or, I'm not sure what pragmas are about, but would there be an easy way to hook {{%translate salutation}} ?
 
-mda
--
You received this message because you are subscribed to a topic in the Google Groups "mustache.java" group.
To unsubscribe from this group and all its topics, send an email to mustachejava...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
 

Sam

unread,
Feb 3, 2016, 1:50:15 PM2/3/16
to mustache.java
You can do this with pragmas but they aren't really designed for that. You would basically return a ValueCode that implements your logic. Alternatively, you can do something like this example that does expression parsing:


Sam
To unsubscribe from this group and all its topics, send an email to mustachejava+unsubscribe@googlegroups.com.

Mark D. Anderson

unread,
Feb 3, 2016, 1:55:36 PM2/3/16
to mustac...@googlegroups.com
Cool. Now if I want to do both this hook and the unbound variable hook, I guess it is up to me to setObjectHandler
once, and internally do both my hooks inside of it?
 
-mda
To unsubscribe from this group and all its topics, send an email to mustachejava...@googlegroups.com.

Sam

unread,
Feb 3, 2016, 1:59:43 PM2/3/16
to mustache.java
Yep. I generally centralize construction of the MustacheFactory and setup everything up there.

Sam
Reply all
Reply to author
Forward
0 new messages