Discussion:
Iterating through a dictionary and internationalization
Pankaj Garg
2011-03-26 18:12:32 UTC
Permalink
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?
--
You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-template-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to json-template+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
Andy Chu
2011-03-26 19:44:56 UTC
Permalink
Post by Pankaj Garg
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.
Post by Pankaj Garg
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
--
You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-template-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to json-template+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
Pankaj Garg
2011-03-27 06:55:06 UTC
Permalink
How do I send a patch ?
I have experience with mercurial, but don't know how to do it on google code
Post by Pankaj Garg
Post by Pankaj Garg
The template provides facility of iterating through lists using
{.repeated
Post by Pankaj Garg
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
Post by Pankaj Garg
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.
{ foo: {k1: v1, k2, v2} }
{.repeated section foo | pairs}
{.end}
result = []
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.
Post by Pankaj Garg
And for internationalization, I saw in the previous group posts that it
can
Post by Pankaj Garg
be done through formatters e.g. {foo| Trans}. But usually there are
strings
Post by Pankaj Garg
(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
--
You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-template-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to json-template+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
Pankaj Garg
2011-03-27 07:15:00 UTC
Permalink
Patch sent: Issue 63
--
You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-template-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to json-template+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
Loading...