我已经按照 django 文档 [1] 在“hello word”类型的谷歌应用引擎网站上实现 i18n。
不幸的是,在阅读了几十个 html 页面、django 和 appengine 文档后,我无法弄清楚发生了什么:
- 将语言代码更改为 django_settings.py 确实更改页面的语言并使用当前语言更新下拉列表(即 LANGUAGE_CODE = 'fr' 或 'es' 或 'en')
- 更改下拉菜单并单击“更改语言”确实会导致“错误 404”并且 URL 显示 http:///i18n/setlang/
我在哪里可以找到应用引擎默认使用的 django 中间件列表?这可能有助于我深入挖掘。
技术要求(我们不打算在 i18n 工作之前升级 :-):
- 蟒蛇2.5.4
- 谷歌应用引擎 1.6.2
- 姜戈 1.2
[1] [https://docs.djangoproject.com/en/1.2/topics/i18n/]
[2] [cssjanus.googlecode.com] 一段代码,正是我想做的事情。但我错过了一个小技巧
index.html 包含这个
<form action="/i18n/setlang/" method="post">
<input name="next" type="hidden" value="/MainPage">
<select name="language">
{% for lang in LANGUAGES %} <option value="{{ lang.0 }}"
{% ifequal LANGUAGE_CODE lang.0 %}
selected="selected"
{% endifequal %}>{{ lang.1 }}</option>
{% endfor %}
</select>
<input type="submit" value="{% trans "Change Language" %}">
</form>
应用程序.yaml:
application: i18n
version: 1
runtime: python
api_version: 1
handlers:
- url: .*
script: helloworld.py`
django_seetings.py
import os
DEBUG = True
TEMPLATE_DEBUG = DEBUG
LANGUAGE_CODE = 'fr'
USE_I18N = True
gettext = lambda s: s
LANGUAGES = (
('en', gettext('English')),
('fr', gettext('French')),
('es', gettext('Spanish')),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
)
SESSION_ENGINE = 'gae_sessions'
helloworld.py(我不使用 urls.py)
# coding=UTF-8
# Standard Python imports.
import os
import sys
import logging
import __builtin__
# Google App Hosting imports.
from google.appengine.dist import use_library
use_library('django', '1.2')
# Enable info logging by the app (this is separate from appserver's
# logging).
logging.getLogger().setLevel(logging.INFO)
# Must set this env var *before* importing any part of Django.
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_settings'
from google.appengine.ext.webapp import util
# Import the part of Django that we use here.
from google.appengine.ext import webapp
from views import MainPage
from django.conf.urls.defaults import include
def main():
# Create a Django application for WSGI
application = webapp.WSGIApplication([('/', MainPage),
(r'^i18n/', include('django.conf.urls.i18n')),
], debug=True)
# Run the WSGI CGI handler with that application.
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
视图.py
'''
Created on Apr 24, 2012
@author:xxxx
'''
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from django.utils.translation import ugettext #ok
from django import http
from django.http import HttpResponseRedirect
import django_settings
from django.utils.translation import check_for_language
#from django.shortcuts import render_to_response
#def MainPage(request):
class MainPage(webapp.RequestHandler):
def get(self):
template_values = {
'helloworld': ugettext("helloworld!"),
'title': ugettext("home page"),
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
#return render_to_response('index.html', template_values)
你好世界.py
# coding=UTF-8
# Standard Python imports.
import os
import sys
import logging
import __builtin__
# Google App Hosting imports.
from google.appengine.dist import use_library
use_library('django', '1.2')
# Enable info logging by the app (this is separate from appserver's
# logging).
logging.getLogger().setLevel(logging.INFO)
# Must set this env var *before* importing any part of Django.
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_settings'
from google.appengine.ext.webapp import util
# Import the part of Django that we use here.
from google.appengine.ext import webapp
from views import MainPage
from django.conf.urls.defaults import include
def main():
# Create a Django application for WSGI
application = webapp.WSGIApplication([('/', MainPage),
(r'^i18n/', include('django.conf.urls.i18n')),
], debug=True)
# Run the WSGI CGI handler with that application.
util.run_wsgi_app(application)
if __name__ == '__main__':
main()