How do you change webapp's encoding to shift_jis?

312 views
Skip to first unread message

guitarift

unread,
Jun 12, 2008, 2:09:49 PM6/12/08
to Google App Engine
I've gone through the documentation but I can't figure out how to
change webapp's response to shift_jis. Any help would be appreciated.

guitarift

unread,
Jun 12, 2008, 8:26:19 PM6/12/08
to Google App Engine
The key to figuring it out was at http://code.google.com/appengine/docs/webapp/buildingtheresponse.html.
I had to add self.response.headers['Content-Type'] = 'text/
html;charset=shift_jis' to my code. I was using notepad++ which gave
me encoding problems. I used Komodo Edit (free software) to force my
app into the shift_jis encoding. Hope this is helpful for someone
else.

Brett Morgan

unread,
Jun 12, 2008, 8:50:47 PM6/12/08
to google-a...@googlegroups.com
Just curious as to why you guys are going with Shift_JIS instead of utf8? Back in '02 when i last played with dealing with the japanese market, we had to deal with three common encodings, but i was hoping that the march of technology would have gotten the market up to using utf8 by now...
--

Brett Morgan http://brett.morgan.googlepages.com/

Francois

unread,
Jun 12, 2008, 9:28:50 PM6/12/08
to Google App Engine
it's probably for access by keitai (japanese mobile phone)
UTF8 support is still sketchy for that, especially in emails

guitarift

unread,
Jul 9, 2008, 8:26:01 PM7/9/08
to Google App Engine
Sorry, didn't know someone replied. utf8 doesn't support the japanese
language very well. The Kanji gets messed up. It wouldn't be too bad
if it was an occasional mess up, but it's terribly bad. According to
wikipedia most japanese sites use shift_jis, I think because a lot of
people use ms windows and it does the job. The rest use euc-jp unix
origin'd stuff especially mobile phones. It looks like app engine
can't handle shift_jis.

Even if I do:

page.content = db.Text("あかさたな", encoding="shift_jis")

I really wish unicode worked as advertised.

Ross Ridge

unread,
Jul 9, 2008, 10:09:07 PM7/9/08
to Google App Engine
guitarift wrote:
> ... It looks like app engine
> can't handle shift_jis.

Google App Engine can handle Shift JIS or just about another encoding
you could name.

> Even if I do:
>
> page.content = db.Text(" $B$"$+$5$?$J (B", encoding="shift_jis")

There's any number of things that you could be doing wrong. Just
from this one line, I can see serveral possible problems. If
"page.content" isn't a property of a datastore entity, you shouldn't
be using db.Text. Instead you should use Python's standard library to
decode and encode strings. Also, in the e-mail you sent above the
string is encoded with 7-bit ISO 2202-JP, not Shift JIS. If you're
going to put Japanese characters in string literatals in your Python
code like this, you'll need to tell the Python interpreter what
encoding you're using or it'll reject it for not using ASCII
characters.

Ross Ridge

guitarift

unread,
Jul 10, 2008, 12:39:35 PM7/10/08
to Google App Engine
Hey Ross,

Thanks for your comments. I might be doing something wrong and
hopefully I can figure out what that is. I'm new to Python so your
comments are invaluable. page.content is a datastore entity.
Unfortunately I can't control the charset this email uses...or can I?
Should I be using Python's standard library to encode/decode strings
even if I am working with datastore entities? I did mess around with
the encode/decode stuff per http://d.hatena.ne.jp/Gemma/20080706/1215331484
but I couldn't get it to work. Thanks.

Chris

On Jul 9, 4:09 pm, Ross Ridge <rri...@csclub.uwaterloo.ca> wrote:
> guitarift wrote:
> > ... It looks like app engine
> > can't handleshift_jis.
>
> Google App Engine can handle Shift JIS or just about another encoding
> you could name.
>
> > Even if I do:
>
> > page.content = db.Text("あかさたな", encoding="shift_jis")
Message has been deleted

Ross Ridge

unread,
Jul 10, 2008, 4:41:06 PM7/10/08
to Google App Engine
guitarift wrote:
> Unfortunately I can't control the charset this email uses...or can I?

What's important is that you're using a consistant character set in
your Python code. If you want to use Shift JIS encoded string
literals (constants) in your code you'll need to make sure that your
editor is set to use Shift JIS, that the Python interpreter knows that
they're Shift JIS strings and that your using the Shift JIS codec to
decode and encode the strings into and from Unicode as necessary. To
tell Python that you're using Shift JIS put the following line at the
start of *all* your Python files:

# coding: shift_jis

(If you're using GNU Emacs or VIM, there are alternative versions of
the above line that you can use instead that will also tell your
editor that you're using Shift JIS)

> Should I be using Python's standard library to encode/decode strings
> even if I am working with datastore entities?

It would probably be better as you would using a consistant method to
convert your strings through out your code. You'll need to decode any
Shift JIS encoded strings into Python Unicode strings before assigning
them to text datastore properties.

> I did mess around with the encode/decode stuff per
> http://d.hatena.ne.jp/Gemma/20080706/1215331484
> but I couldn't get it to work. Thanks.

I don't read Japanese but that page has "coding: utf-8" on it which if
you're using tells the Python interpretter that your source code is
encoded with UTF-8 rather than Shift JIS.

Ross Ridge
Reply all
Reply to author
Forward
0 new messages