Python 如何在GAE中使用django翻译?

Python 如何在GAE中使用django翻译?,python,django,google-app-engine,django-1.5,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 ... env_variables: DJANGO_SETT

我有以下设置-

文件夹结构

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

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

libraries:
- name: django
  version: "1.5"
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',
)
{% load i18n %}
...
{% trans "This is the title." %}
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)
应用程序yaml

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

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

libraries:
- name: django
  version: "1.5"
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',
)
{% load i18n %}
...
{% trans "This is the title." %}
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)
设置.py

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

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

libraries:
- name: django
  version: "1.5"
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',
)
{% load i18n %}
...
{% trans "This is the title." %}
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)
index.html

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

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

libraries:
- name: django
  version: "1.5"
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',
)
{% load i18n %}
...
{% trans "This is the title." %}
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)
main.py

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

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

libraries:
- name: django
  version: "1.5"
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',
)
{% load i18n %}
...
{% trans "This is the title." %}
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)

但在结果
“这是标题。”
以英文显示。我的设置(或文件位置)有什么问题?

您的区域设置目录是指向翻译文件的绝对路径,而您当前的设置告诉Django查找文件系统的根目录

尝试以下方法将Django指向正确的路径:

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

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

我无意中发现了这个回购协议,其中有一个如何让GAE与Django i18n合作的例子:

请让我知道这是否有帮助

编辑2:

尝试将语言移动到设置中的区域设置路径下。并添加所有

以及在呈现模板时强制Django使用某种语言

您还可以使用此标记告诉您Django有哪些可用语言:

{% get_available_languages %}

当您编写
template\u values={}
时,这是否意味着您正在使用空字典格式化/呈现html页面?在
.render
之后它的值是多少?如果您将模板值更改为:
template\u values={“trans”:“ru”}
?@tadhgmdonald Jensen,在文档中没有看到模板变量影响语言。但是我尝试添加了
{“trans”:“ru”}
-这没有帮助。在GAE上搜索i18n,您是否运行了“python manage.py”makemessages和compilemessages?您是否在项目中部署区域设置目录?@davka,当然,消息已编译(上面的树包含相应的文件)。我会再查一下目录的。不幸的是,这没用。我已经更新了这个问题。我从google.appengine.ext.webapp导入
模板,而不是
django.template
。这可能是问题的原因吗?@lauu请检查编辑,如果有帮助,请告诉我。谢谢,亚历克斯。这里给出的示例应用程序运行良好,但它使用jinja2而不是django。。。