anatoly techtonik
anatoly techtonik

Reputation: 20531

Hack Jinja2 to encode from `utf-8` instead of `ascii`?

Jinja2 converts all template variables into unicode before processing. Can anybody find a place where does this happen?

The problem is that it assumes that strings are ascii, but we (at Roundup) are using utf-8 internally and our ORM (HyperDB) restores object properties to utf-8 automatically, and converting them all into unicode in every view just before passing to templates is too much legwork.

Upvotes: 3

Views: 3988

Answers (1)

anatoly techtonik
anatoly techtonik

Reputation: 20531

Answer from Armin:

Unfortunately that is impossible. Jinja uses the default string coercion on 2.x that Python provides for speed. There are no guaranteed calls to make something unicode. The only shitty choice you have is to reload sys and call sys.setdefaultencoding('utf-8') or something.

UPDATE: Jinja2 2.8 contains some updates related to implicit string conversions. This gives me the idea that it is possible to go without sys.setdefaultencoding('utf-8') by overriding __add__ methods of the unicode type and make sure that it is type is used first while concatenating strings.

https://github.com/mitsuhiko/jinja2/issues/511

Upvotes: 2

Related Questions