简体   繁体   English

如何使用GAE进行django翻译?

[英]How to use django translation with GAE?

I have the following setup - 我有以下设置 -

folders structure : 文件夹结构

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 : 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 : 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 : index.html

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

and main.py : main.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)

But in result "This is the title." 但结果"This is the title." is displayed in English. 以英文显示。 What is wrong with my setup (or files location)? 我的设置(或文件位置)有什么问题?

You LOCALE_DIRS are absolute paths to your translations files and your current setup is telling Django to look in the root of the file system. LOCALE_DIRS是您的翻译文件的绝对路径,您当前的设置告诉Django查看文件系统的根目录。

Try something like this to point Django to the correct path: 尝试这样的方法将Django指向正确的路径:

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

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

EDIT: 编辑:

I stumbled on this repo that has an example of how to get GAE to work with Django i18n: https://github.com/googlearchive/appengine-i18n-sample-python 我偶然发现了这个repo,它有一个如何让GAE与Django i18n一起工作的例子: https//github.com/googlearchive/appengine-i18n-sample-python

Please let me know if this helps 请让我知道这可不可以帮你

EDIT 2: 编辑2:

Try moving your LANGUAGES below your LOCALE_PATHS in your settings. 尝试在您的设置中将LANGUAGES移动到LOCALE_PATHS下方。 And add all the middlewares listed here 并添加此处列出的所有中间件

And to force Django to use a certain language when rendering a template use this example 并且在渲染模板时强制Django使用某种语言使用此示例

You can also use this tag to tell you which languages Django has available: 您也可以使用此标记告诉您Django可用的语言:

{% get_available_languages %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM