如何在GAE中使用django翻译?

时间:2022-03-27 19:21:05

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." is displayed in English. What is wrong with my setup (or files location)?

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

2 个解决方案

#1


1  

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-web-python

Please let me know if this helps

如果有用的话,请告诉我

EDIT 2:

编辑2:

Try moving your LANGUAGES below your LOCALE_PATHS in your settings. And add all the middlewares listed here

尝试在设置中将语言移动到LOCALE_PATHS下面。并添加这里列出的所有中间件

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 %}

#2


0  

check this blog post, I hope this helps

查看这篇博文,我希望这能有所帮助

http://blog.yjl.im/2009/02/using-django-i18n-in-google-app-engine.html

#1


1  

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-web-python

Please let me know if this helps

如果有用的话,请告诉我

EDIT 2:

编辑2:

Try moving your LANGUAGES below your LOCALE_PATHS in your settings. And add all the middlewares listed here

尝试在设置中将语言移动到LOCALE_PATHS下面。并添加这里列出的所有中间件

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 %}

#2


0  

check this blog post, I hope this helps

查看这篇博文,我希望这能有所帮助

http://blog.yjl.im/2009/02/using-django-i18n-in-google-app-engine.html