I tried using response.charset as used by Pylons (http://
pylonsbook.com/en/1.1/unicode.html#output-encoding) and WebOb, but the
expose decorator overrides that (since http://trac.turbogears.org/ticket/1480):
In decorators.py
if 'charset' not in content_type and (
content_type.startswith('text') or content_type ==
'application/json'):
content_type = '%s; charset=utf-8' % content_type
First, shouldn't that be
content_type = '%s; charset=%s' % (content_type,
response.charset or 'utf-8')
?
Second, if you have set a charset with
response.headers['Content-type'] = 'text/plain; charset=mycharset'
should Turbogears really chop it off with
content_type = controller_content_type.split(';')[0]
?
Done with questions, here is a workaround, as suggested by
DreadPirateBob on IRC:
# consider that charset could be set to "", then we do not
append a default charset; we do on None
if not response.charset == '' and 'charset' not in
content_type and (
content_type.startswith('text') or content_type ==
'application/json'):
content_type = '%s; charset=%s' % (content_type,
response.charset or 'utf-8')
The problem is how to determine if response.charset is None because
the user forgot to set it ant wants it to be set automatically for
json and text/... and and None because it is set explicitly. As
"Content-Type: something; charset=" seems to not be valid HTTP, this
enables to set response.charset = '' in our controller to render the
content type without a charset.
I don't think it is a very clean solution because having different
results for "" and None is not intuitive at all, but it seems to be a
workaround that does not break existing code. Something like a
response.charset=AUTO singleton and None for no charset might be
cleaner.
What do you think?
(To justify why one would like to unset the charset at all:
"Some" (currently all webkit) browsers do not allow charset in a
content type for text/event-stream and the web developer should be
able to unset it anyway if required, even if it is only for legacy
testing purposes.)
On Dec 22, 11:22 pm, nh2 <[email protected]> wrote:
> After having changed the response Content-Type with
>
> response.headers['Content-type'] = 'text/event-stream'
>
> as described inhttp://turbogears.org/2.1/docs/main/ResponseTypes.html,
> I would like to manually change the charset or even delete it.
> Unfortunately, Turbogears 2.1 chops off the charset I set (tg/
> decorators.py:139) and always appends a "; charset=utf-8" (tg/
> decorators.py:150).
>
> Is there a way to specify explicitly or unset the charset?
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en.