How to use django translation with GAE?
Asked Answered
F

1

9

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:

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

and 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)?

Foehn answered 24/1, 2016 at 18:38 Comment(6)
when you write template_values = {} doesn't that mean you are using empty dictionary to format/render the html page? what is it's value after the .render? what if you change template_values to something like: template_values = {"trans":"ru"}?Whitford
@TadhgMcDonald-Jensen, haven't seen in the documentation that template variables effects the language. But I've tried to add {"trans":"ru"} - it didn't help.Foehn
search for i18n on GAE, found thisEmpathic
Did you run "python manage.py" makemessages and compilemessages? Are you deploying the locale directories with your project?Wendy
@davka, surely, messages are compiled (the tree above contains according files). I'll check again about the directories.Foehn
@Allen, do you suggest to use different package (instead of django's translation)?Foehn
B
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.

Try something like this to point Django to the correct path:

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

Please let me know if this helps

EDIT 2:

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

And to force Django to use a certain language when rendering a template use this example

You can also use this tag to tell you which languages Django has available:

{% get_available_languages %}
Buchholz answered 28/1, 2016 at 19:9 Comment(3)
It didn't help, unfortunately. I've updated the question. I import from google.appengine.ext.webapp import template instead of django.template. Could it be cause the problem?Foehn
@Foehn please check edit and let me know if that helps.Buchholz
Thanks, Alex. The sample app given there works well, but it uses jinja2 instead of django...Foehn

© 2022 - 2024 — McMap. All rights reserved.