librelist archives

« back to archive

Jinja's i18n extension and Babel extraction

Jinja's i18n extension and Babel extraction

From:
Julen Ruiz Aizpuru
Date:
2011-04-08 @ 08:22
Hello,

Before anything, let me describe my scenario:

My Flask application is using a native XML database in the backend for 
storing data represented in XML. The data is retrieved and transformed 
using XQuery, and the result of the transformation is a Jinja template 
which I render afterwards (by using render_template_string).

In XQuery the curly braces indicate that the surrounded text needs to be 
evaluated as XQuery. Thus, in order to use Jinja templating code in the 
transformed output I need to escape the curly braces by doubling them: 
instead of {{ _('foo') }} I have to use {{{{ _('foo') }}}}.

This works fine at runtime, but the problem arises when I need to 
extract localizable strings from the XQuery transformations. It seems 
that the babel_extract[1] method of Jinja's i18n extension only looks up 
for double braces, and there doesn't seem to be a way to define custom 
delimiters for extraction.

Does somebody know how can I customize this? Any other possible workarounds?

Thanks in advance.


[1] https://github.com/mitsuhiko/jinja2/blob/master/jinja2/ext.py#L540

Re: [flask] Jinja's i18n extension and Babel extraction

From:
Kieran Darcy
Date:
2011-04-08 @ 09:21
I've never done it myself, but you could try specifying your 
"variable_start_string" and "variable_end_string" in the Jinja environment.
http://jinja.pocoo.org/docs/api/#high-level-api

from flask import Flask, render_template_string

app = Flask(__name__)
app.jinja_env.variable_start_string = '<%'
app.jinja_env.variable_end_string = '%>'

@app.route("/")
def hello():
     greeting = 'Hello World!'
     return render_template_string("<% greeting %>")



On 08/04/2011 09:22, Julen Ruiz Aizpuru wrote:
> Hello,
>
> Before anything, let me describe my scenario:
>
> My Flask application is using a native XML database in the backend for
> storing data represented in XML. The data is retrieved and transformed
> using XQuery, and the result of the transformation is a Jinja template
> which I render afterwards (by using render_template_string).
>
> In XQuery the curly braces indicate that the surrounded text needs to be
> evaluated as XQuery. Thus, in order to use Jinja templating code in the
> transformed output I need to escape the curly braces by doubling them:
> instead of {{ _('foo') }} I have to use {{{{ _('foo') }}}}.
>
> This works fine at runtime, but the problem arises when I need to
> extract localizable strings from the XQuery transformations. It seems
> that the babel_extract[1] method of Jinja's i18n extension only looks up
> for double braces, and there doesn't seem to be a way to define custom
> delimiters for extraction.
>
> Does somebody know how can I customize this? Any other possible workarounds?
>
> Thanks in advance.
>
>
> [1] https://github.com/mitsuhiko/jinja2/blob/master/jinja2/ext.py#L540

Re: [flask] Jinja's i18n extension and Babel extraction

From:
Julen Ruiz Aizpuru
Date:
2011-04-08 @ 10:16
or., 2011.eko apiren 08a 11:21(e)an, Kieran Darcy(e)k idatzi zuen:
> I've never done it myself, but you could try specifying your
> "variable_start_string" and "variable_end_string" in the Jinja environment.
> http://jinja.pocoo.org/docs/api/#high-level-api
>
> from flask import Flask, render_template_string
>
> app = Flask(__name__)
> app.jinja_env.variable_start_string = '<%'
> app.jinja_env.variable_end_string = '%>'
>
> @app.route("/")
> def hello():
>       greeting = 'Hello World!'
>       return render_template_string("<% greeting %>")
>
>

Yeah, I thought about this but wasn't quite happy changing the delimiter 
string though.
But if there's no other workaround for this I'll have to go that way.

Thanks Kieran.

Re: [flask] Jinja's i18n extension and Babel extraction

From:
Simon Sapin
Date:
2011-04-08 @ 10:19
Le 08/04/2011 12:16, Julen Ruiz Aizpuru a écrit :
> Yeah, I thought about this but wasn't quite happy changing the delimiter
> string though.
> But if there's no other workaround for this I'll have to go that way.
>
> Thanks Kieran.

Hi,

What about changing the delimiters to {{{{ and }}}} for Babel extraction ?

Regards,
-- 
Simon Sapin
http://exyr.org/

Re: [flask] Jinja's i18n extension and Babel extraction

From:
Julen Ruiz Aizpuru
Date:
2011-04-08 @ 10:23
or., 2011.eko apiren 08a 12:19(e)an, Simon Sapin(e)k idatzi zuen:
> Le 08/04/2011 12:16, Julen Ruiz Aizpuru a écrit :
>> Yeah, I thought about this but wasn't quite happy changing the delimiter
>> string though.
>> But if there's no other workaround for this I'll have to go that way.
>>
>> Thanks Kieran.
>
> Hi,
>
> What about changing the delimiters to {{{{ and }}}} for Babel extraction ?
>

That was my initial question; how can you achieve that?

Note that at the same time I need to be able to extract from the 
original {{ }} delimiters too.

Re: [flask] Jinja's i18n extension and Babel extraction

From:
Simon Sapin
Date:
2011-04-08 @ 10:37
Le 08/04/2011 12:23, Julen Ruiz Aizpuru a écrit :
> That was my initial question; how can you achieve that?

I’m not familiar with Babel. When you do extraction, is the Jinja 
Environment exposed? If so, change the delimiters there. If not, I’d 
look at Babel docs or source to see where the Environment is created and 
where to hook some custom code/options.

> Note that at the same time I need to be able to extract from the
> original {{ }} delimiters too.

Take a chance and try setting each delimiter to a list instead of just 
one string, but it’s not documented so I doubt it’ll work. You might 
need two passes.

Regards,
-- 
Simon Sapin
http://exyr.org/

Re: [flask] Jinja's i18n extension and Babel extraction

From:
Julen Ruiz Aizpuru
Date:
2011-04-11 @ 07:51
or., 2011.eko apiren 08a 12:37(e)an, Simon Sapin(e)k idatzi zuen:
> Le 08/04/2011 12:23, Julen Ruiz Aizpuru a écrit :
>> That was my initial question; how can you achieve that?
>
> I’m not familiar with Babel. When you do extraction, is the Jinja
> Environment exposed? If so, change the delimiters there. If not, I’d
> look at Babel docs or source to see where the Environment is created and
> where to hook some custom code/options.

For the record, this is how I got his working:

1. Choose the new delimiters. I've used [[ and ]], since curly braces 
need to be escaped and < and > characters can confuse the XQuery parser 
with XML/HTML tags.

2. Set the delimiters in the application initialization function:

     app.jinja_env.variable_start_string = '[['
     app.jinja_env.variable_end_string = ']]'

3. Set the delimiters in the babel.cfg file. This ensures the jinja 
environment will have the defined options when running the extraction:

[jinja2: **/templates/**.html]
extensions = jinja2.ext.autoescape,jinja2.ext.with_
variable_start_string = [[
variable_end_string = ]]


>> Note that at the same time I need to be able to extract from the
>> original {{ }} delimiters too.
>
> Take a chance and try setting each delimiter to a list instead of just
> one string, but it’s not documented so I doubt it’ll work. You might
> need two passes.
>

Right, this doesn't work.


Again, thanks all for your help.