python-babel / flask-babel

i18n and l10n support for Flask based on Babel and pytz
https://python-babel.github.io/flask-babel/
Other
444 stars 159 forks source link

UnicodeDecodeError #48

Closed cmcilvoy closed 8 years ago

cmcilvoy commented 10 years ago

UnicodeDecodeError: 'ascii' codec can't decode byte...

Environment: Python=3.3.2 Flask=0.10.1 Flask-Babel=0.9

Any attempt to read translations directory files (.po or .mo) result in UnicodeDecodeError. Works as expected with Python 2.7.5.

dahlia commented 10 years ago

More detailed traceback is needed to reproduce the bug.

ael-code commented 9 years ago

In libreant we encountered the same issue:

Babel==1.3
Flask==0.10.1
Flask-Babel==0.9
Jinja2==2.7.3
MarkupSafe==0.23

This is the Traceback:

Traceback (most recent call last):
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/libreant/libreant/webant/webant.py", line 64, in index
    return render_template('index.html')
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/libreant/libreant/webant/templates/index.html", line 3, in top-level template code
    {% import 'searchbar.html' as searchbar %}
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask_Bootstrap-3.3.4.1-py2.7.egg/flask_bootstrap/templates/bootstrap/base.html", line 1, in top-level template code
    {% block doc -%}
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask_Bootstrap-3.3.4.1-py2.7.egg/flask_bootstrap/templates/bootstrap/base.html", line 4, in block "doc"
    {%- block html %}
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask_Bootstrap-3.3.4.1-py2.7.egg/flask_bootstrap/templates/bootstrap/base.html", line 20, in block "html"
    {% block body -%}
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Flask_Bootstrap-3.3.4.1-py2.7.egg/flask_bootstrap/templates/bootstrap/base.html", line 21, in block "body"
    {% block navbar %}
  File "/home/libreant/libreant/webant/templates/index.html", line 11, in block "navbar"
    {{navbar.navbar(search=False)}}
  File "/home/libreant/libreant/webant/templates/navbar.html", line 17, in template
    <a href="{{url_for('add')}}">{%trans%}Add item{%endtrans%}</a>
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/Jinja2-2.7.3-py2.7.egg/jinja2/ext.py", line 140, in gettext
    rv = Markup(rv)
  File "/home/libreant/libreant/ve/lib/python2.7/site-packages/MarkupSafe-0.23-py2.7-linux-x86_64.egg/markupsafe/__init__.py", line 74, in __new__
    return text_type.__new__(cls, base)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)

this is the Serbian message.po file that break every things:

#: /home/libreant/libreant/webant/templates/search.html:6
#: /home/libreant/libreant/webant/templates/searchbar.html:9
msgid "Search"
msgstr "Тражи"
ael-code commented 9 years ago

I've investigated a bit. Seems that the problem belongs to jinjia2 and the autoescape function. I'll open an issue on that project.

TkTech commented 8 years ago

According to the jinja ticket, this was a problem on a specific version of IronPython. A workaround is provided in that ticket.