Iterating through a dictionary and internationalization

18 views
Skip to first unread message

Pankaj Garg

unread,
Mar 26, 2011, 2:12:32 PM3/26/11
to json-t...@googlegroups.com
The template provides facility of iterating through lists using {.repeated section foo} 
But no such option is there for iterating through a dictionary, as is present in python 
 
for key, value in foo.iteritems()

It will be very helpful, if such an option is present in the template, and would make transition from other templates to "json-template" a lot more easier.
 
And for internationalization, I saw in the previous group posts that it can be done through formatters e.g. {foo| Trans}. But usually there are strings (not variables) which are required to be translated. Is there any way of making that possible?

Andy Chu

unread,
Mar 26, 2011, 3:44:56 PM3/26/11
to json-t...@googlegroups.com, Pankaj Garg
On Sat, Mar 26, 2011 at 11:12 AM, Pankaj Garg <pank...@gmail.com> wrote:
> The template provides facility of iterating through lists using {.repeated
> section foo}
> But no such option is there for iterating through a dictionary, as is
> present in python
>
>
> for key, value in foo.iteritems()
>
> It will be very helpful, if such an option is present in the template, and
> would make transition from other templates to "json-template" a lot more
> easier.

I've thought about this -- the easiest way in Python would be to make
use of the "section formatters", which I added pretty recently. They
are just regular formatters, but they can return a list. And they can
be applied to repeated sections.


So if you have:

{ foo: {k1: v1, k2, v2} }

You should be able to do:

{.repeated section foo | pairs}
{@key} {@value}
{.end}

Where "pairs" is a function like this:

def Pairs(data):
result = []
for k, v in data.iteritems():
result.append({'@key': k, '@value': v})
return result

The dictionary node is transformed to a list node right before iteration.

I think this should probably be built in to the python version -- if
it works for you send me a patch.


> And for internationalization, I saw in the previous group posts that it can
> be done through formatters e.g. {foo| Trans}. But usually there are strings
> (not variables) which are required to be translated. Is there any way of
> making that possible?

It depends what your workflow is like. It's hard to think of a scheme
which isn't error prone... the most robust method seems to be to have
a "database" of all strings, but I realize that most web apps don't
have that.

It's easy to parse templates and generate them. In fact I think I
even exposed the parse/tokenizer for the syntax highlighter. So if
you have an English template, you can parse it and pick out the
literal strings, and then translate those strings, and assemble
another template.

I guess this is done at "build time" rather than at template expansion
time -- you have a set of static templates for each language.

Perhaps a way of making this less error prone would be to have some
markers like this

{#trans}This is a string that has a translation{#end}

{#comment} is a comment and will already work. So then your parser
can identify literal strings identified by the template author,
instead of trying to guess at all the string boundaries.

I don't know if there are any better schemes; open to suggestions.


Andy

Pankaj Garg

unread,
Mar 27, 2011, 2:55:06 AM3/27/11
to Andy Chu, json-template
How do I send a patch ?
I have experience with mercurial, but don't know how to do it on google code

Pankaj Garg

unread,
Mar 27, 2011, 3:15:00 AM3/27/11
to Andy Chu, json-template
Patch sent: Issue 63
Reply all
Reply to author
Forward
0 new messages