python - 如何在 GAE 中使用 django 翻译?

标签 python django google-app-engine django-1.5

我有以下设置 -

文件夹结构:

myapp
  - conf
    - locale
      - ru
        - LC_MESSAGES
          - django.mo # contains "This is the title." translation
          - django.po
  - templates
    - index.html
  setting.py
  main.py

app.yaml:

...
env_variables:
 DJANGO_SETTINGS_MODULE: 'settings'

handlers:
...
- url: /locale/ # do I need this?
  static_dir: templates/locale

libraries:
- name: django
  version: "1.5"

settings.py:

USE_I18N = True

LANGUAGES = (
    ('en', 'EN'),
    ('ru', 'RU'),
)

LANGUAGE_CODE = 'ru'
LANGUAGE_COOKIE_NAME = 'django_language'

SECRET_KEY = 'some-dummy-value'

MIDDLEWARE_CLASSES = (
  'django.middleware.locale.LocaleMiddleware'
)

LOCALE_PATHS = (
    '/locale',
    '/templates/locale',
)

index.html:

{% load i18n %}
...
{% trans "This is the title." %}

ma​​in.py:

from google.appengine.ext.webapp import template
...
translation.activate('ru')
template_values = {}
file_template = template.render('templates/index.html', template_values)
self.response.out.write(file_template)

但结果 "This is the title." 显示为英文。我的设置(或文件位置)有什么问题?

最佳答案

您的 LOCALE_DIRS 是您的翻译文件的绝对路径,您当前的设置告诉 Django 在文件系统的根目录中查找。

尝试像这样将 Django 指向正确的路径:

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))

LOCALE_PATHS = (
    os.path.join(PROJECT_PATH, 'conf/locale'),
)

编辑:

我偶然发现了这个 repo,它有一个如何让 GAE 与 Django i18n 一起工作的例子:https://github.com/googlearchive/appengine-i18n-sample-python

如果有帮助,请告诉我

编辑 2:

尝试在您的设置中将您的 LANGUAGES 移到您的 LOCALE_PATHS 下面。并添加所有 middlewares listed here

并强制 Django 在呈现模板时使用某种语言 use this example

你也可以使用这个标签来告诉你 Django 有哪些可用的语言:

{% get_available_languages %}

关于python - 如何在 GAE 中使用 django 翻译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34979813/

相关文章:

python - 如何在python opencv中获取hsv颜色的低值和高值

python - 仅将字符串拆分为两部分

python - 导入错误: No module named 'app'

python - 如果我使用 mod_wsgi 来提供应用程序,在哪里可以看到标准输出?

python - 在 Django 中修改 Active Directory 用户

google-app-engine - ndb async 是否保证在应用程序请求完成后执行?

python - 我的小部件不想消失

python - 如何修复 https openid 错误

python - Google App Engine 和 Django 模板 : why do these two cases differ?

google-app-engine - 谷歌应用引擎 : this application does not exist