Sample code of i18n on GAE+Django

63 views
Skip to first unread message

A.TNG

unread,
Sep 6, 2008, 10:21:57 AM9/6/08
to google-a...@googlegroups.com
Hi All,

Finally, I could make i18n work on GAE now (with built-in Django
0.96.1). Please check attached ZIP file. Set LANGUAGE_CODE in
settings.py to get different locale.

Enjoy. :-)

--
Best Regards,
TANG Jiyu

i18nsample.zip

Ping Chen

unread,
Sep 6, 2008, 10:26:18 AM9/6/08
to A. TNG, google-a...@googlegroups.com

Good job!

2008/9/6 A. TNG <tang...@gmail.com>



--
Anything is possible,I believe!
http://www.cpedia.com
Message has been deleted

A.TNG

unread,
Sep 6, 2008, 11:01:34 AM9/6/08
to google-a...@googlegroups.com
On 9/6/08, Davide Rognoni <davide....@gmail.com> wrote:
>
> How can you add "i18sample.zip" to the post?

I just want to share the sample code. Anything wrong? File size is only 9k.

>> i18nsample.zip
>> 9KViewDownload

Message has been deleted

A.TNG

unread,
Sep 6, 2008, 10:12:26 PM9/6/08
to google-a...@googlegroups.com
On Sun, Sep 7, 2008 at 5:29 AM, Davide Rognoni <davide....@gmail.com> wrote:
>
> Yes you can :-) but I don't know where is the [button] to add files.
> I'm posting from web...

I use Gmail to check this group. So I could send a email to this group
with attachments.
>
> On Sep 6, 5:01 pm, A.TNG <tang.j...@gmail.com> wrote:

Pierre

unread,
Sep 7, 2008, 3:07:43 AM9/7/08
to Google App Engine
Hi,

I must be a fool. I decompresses your zip then run ./dev_appserver
i18nsample and it worked 'translated 'Hello' to 'Bonjour' ;-)

So, :
- I take your setting.py and put it in my application's directory.
(Note I already have django.po in 'myapp/locale/fr/LC_MESSAGES/
django.po' and ran compile-messages)
- run ./dev_appserver

Nothing ... ???/.§?§ No translation.

Any hint ?

Pierre

On 7 sep, 04:12, A.TNG <tang.j...@gmail.com> wrote:
Message has been deleted

A.TNG

unread,
Sep 7, 2008, 7:44:49 PM9/7/08
to google-a...@googlegroups.com
On Sun, Sep 7, 2008 at 3:07 PM, Pierre <pierre....@gmail.com> wrote:
>
> Hi,
>
> I must be a fool. I decompresses your zip then run ./dev_appserver
> i18nsample and it worked 'translated 'Hello' to 'Bonjour' ;-)
>
> So, :
> - I take your setting.py and put it in my application's directory.
> (Note I already have django.po in 'myapp/locale/fr/LC_MESSAGES/
> django.po' and ran compile-messages)
> - run ./dev_appserver
>
> Nothing ... ???/.§?§ No translation.
>
> Any hint ?

I have no idea. Could you send me your project? Maybe I will find any clue.

A.TNG

unread,
Sep 9, 2008, 12:41:24 AM9/9/08
to google-a...@googlegroups.com
Hi,

I find another way to use django's i18n middleware.

The story is that you already have a app based on webapp framework and
you want to make it support i18n.

1. you still need to create your .po and .mo files and put them under
appproj/locale/{locale, eg: zh_CN}/LC_MESSAGES/
2. add settings.py to appproj/
3. modify your main.py and import several django modules.
4. Now, you could use _() in .py and {% trans "" %} in template. You
could follow your webapp way, no need to change to django way.

For detail information, you could check attached code sample. Enjoy :-)

webappi18n.zip

Pierre

unread,
Sep 9, 2008, 2:24:36 AM9/9/08
to Google App Engine
Hi Jiyu,

This definitively works !! and Rocks !! ;-)

Just tried in real world :
- extract
- run appcfg.py update webappi18n
- browse

--> Hello is transformed to "Bonjour" and that's pretty good !
Voilà !

Thank to you.

Pierre
> webappi18n.zip
> 7KViewDownload

Mathieu

unread,
Sep 14, 2008, 11:53:04 PM9/14/08
to Google App Engine
Hi TNG,

I used your sample code and I have been able to create localization
files for my application and make them work properly.

The only problem I have now is that I dont know how to switch between
each languages without hard-coding it in the settings file.

I want users to be able to choose the locale they want to use.

The only way I have read about involves the django middleware, but I
dont believe I could succceed in making it work. I have failed over
and over at moving my app to django 1.0

Thanks
>  webappi18n.zip
> 7KViewDownload

A.TNG

unread,
Sep 22, 2008, 10:00:02 AM9/22/08
to google-a...@googlegroups.com
On Mon, Sep 15, 2008 at 11:53 AM, Mathieu <mat...@mathieu-sylvain.net> wrote:
>
> Hi TNG,
>
> I used your sample code and I have been able to create localization
> files for my application and make them work properly.
>
> The only problem I have now is that I dont know how to switch between
> each languages without hard-coding it in the settings file.
>
> I want users to be able to choose the locale they want to use.
>
> The only way I have read about involves the django middleware, but I
> dont believe I could succceed in making it work. I have failed over
> and over at moving my app to django 1.0

Originally in Django, locale could be set via session. But GAE cannot
work with session in Django. So I tried to set
settings.LANGUAGE_CODE='zh-cn' directly to make i18n works
dynamically. It works.

Maybe there are some other ways.

I never tried Django 1.0. All above is based on GAE built-in Django 0.96.1.

Alex

unread,
Oct 17, 2008, 11:19:41 AM10/17/08
to Google App Engine
On Sep 22, 4:00 pm, A.TNG <tang.j...@gmail.com> wrote:
> Originally in Django, locale could be set via session. But GAE cannot
> work with session in Django. So I tried to set
> settings.LANGUAGE_CODE='zh-cn' directly to make i18n works
> dynamically. It works.

I have done so in webappi18n.zip but got the this error msg

AttributeError: 'NoneType' object has no attribute 'LANGUAGE_CODE'

No matter where I have inserted this directive.

Alex

unread,
Oct 17, 2008, 8:02:32 AM10/17/08
to Google App Engine
On Sep 22, 4:00 pm, A.TNG <tang.j...@gmail.com> wrote:
> Originally in Django, locale could be set via session. But GAE cannot
> work with session in Django. So I tried to set
> settings.LANGUAGE_CODE='zh-cn' directly to make i18n works
> dynamically. It works.

I have extended the example "webappi18n" with
"settings.LANGUAGE_CODE='zh-cn'" inserting it after
"self.response.out.write(_('hello'))" and got this error:
AttributeError: 'NoneType' object has no attribute 'LANGUAGE_CODE'.

Alex

unread,
Oct 19, 2008, 8:41:47 PM10/19/08
to Google App Engine
On Sep 22, 4:00 pm, A.TNG <tang.j...@gmail.com> wrote:
> Originally in Django, locale could be set via session. But GAE cannot
> work with session in Django. So I tried to set
> settings.LANGUAGE_CODE='zh-cn' directly to make i18n works
> dynamically. It works.

I have found some interesting information regarding the "settings
object":
"
*Altering settings at runtime*
You shouldn’t alter settings in your applications at runtime.
[...] The only place you should assign to settings is in a settings
file.
"
See http://www.djangoproject.com/documentation/0.96/settings/ for more
information!

Btw Tang, as I have already told you in a mail, I could not set the
LANGUAGE_CODE *dynamically*.
E.g.
settings.LANGUAGE_CODE = self.request.query_string
does not work and after I have read the information above, the
settings should not be set this way.

Best Regards
Alex

A.TNG

unread,
Oct 20, 2008, 4:00:41 AM10/20/08
to google-a...@googlegroups.com

Oh, I made a mistake. I'm not familiar with Django. So, I thought it
would work at first.

Now, I have no idea how to make i18n work dynamically. If I in a pure
Django Environment, session, cookie are both used to change language.
But, How could it be done in webapp framework?

Do you have any idea? I meet some similar problems in my project too.

>
> Best Regards
> Alex

Alex

unread,
Oct 23, 2008, 8:03:17 PM10/23/08
to Google App Engine
On Oct 20, 10:00 am, A.TNG <tang.j...@gmail.com> wrote:
> Do you have any idea? I meet some similar problems in my project too.

I have investigated this issue and put a lot of time (too much!)
tinkering to get it working but without success (found a really dirty
hack to get it working with dev_appserver.py *locally*). However I
have killed this task in my project to be able to go on with other
tasks.

I will post a corresponding solution here if I find a way for setting
LANGUAGE_CODE *dynamically*. Maybe this issue is also unsolvable
without a hack in GAE API version v.1... Looking forward for GAE API v.
2 :)

Best Regards
Alex

Ary Manzana

unread,
Oct 30, 2008, 8:57:41 PM10/30/08
to Google App Engine
Hi,

Thanks all for the big effort you are putting in getting i18n into
GAE.

Did anyone find a way to choose a language dynamically?

All the best,
Ary

N. Rosencrantz

unread,
Nov 19, 2008, 1:20:03 AM11/19/08
to Google App Engine
Altering settings.LANGUAGE_CODE at runtime switches languages
dynamically (while the django manual says not to i.e. "You shouldn’t
alter settings in your applications at runtime")
e.g. english for .com and pt_BR for .com.br:

from django.conf import settings
if os.environ.get('HTTP_HOST'):
url = os.environ['HTTP_HOST']
else:
url = os.environ['SERVER_NAME']
if url.endswith('domain1.com'):
settings.LANGUAGE_CODE = 'en'
elif url.endswith('domain2.com.br'):
settings.LANGUAGE_CODE = 'pt_BR'

Manual pages say "don't do this." The app does however dynamically
choose between english and portuguese depending on the URL this way. I
don't know why the manual advises against. Maybe performance reasons.

best regards
Niklas

Alex

unread,
Jan 4, 2009, 11:54:40 AM1/4/09
to Google App Engine
On Nov 19 2008, 7:20 am, niklasr <nikla...@gmail.com> wrote:
> from django.conf import settings
> if os.environ.get('HTTP_HOST'):
>   url = os.environ['HTTP_HOST']
> else:
>   url = os.environ['SERVER_NAME']
> if url.endswith('domain1.com'):
>   settings.LANGUAGE_CODE = 'en'
> elif url.endswith('domain2.com.br'):
>   settings.LANGUAGE_CODE = 'pt_BR'

An even cleaner solution would be to use the "Accept-Language" header
value which you can get by calling the following command:

self.request.headers.get('Accept-Language')

It retrieves the users preferred language specified in the browser
settings. The returned value can be also a prioritized list of
languages like:

en_US,en,de,ru
Reply all
Reply to author
Forward
0 new messages