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

LA_
  • 19,823
  • 58
  • 172
  • 308
  • 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"}`? – Tadhg McDonald-Jensen Jan 24 '16 at 18:48
  • @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. – LA_ Jan 25 '16 at 19:05
  • search for i18n on GAE, [found this](http://webapp-improved.appspot.com/api/webapp2_extras/i18n.html#api-webapp2-extras-i18n) – Allen Jan 27 '16 at 15:43
  • Did you run "python manage.py" makemessages and compilemessages? Are you deploying the locale directories with your project? – dkarchmer Jan 28 '16 at 04:09
  • @davka, surely, messages are compiled (the tree above contains according files). I'll check again about the directories. – LA_ Jan 28 '16 at 08:10
  • @Allen, do you suggest to use different package (instead of django's translation)? – LA_ Jan 28 '16 at 08:12

1 Answers1

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 %}
Alex Carlos
  • 882
  • 5
  • 7
  • 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? – LA_ Jan 29 '16 at 17:24
  • @LA_ please check edit and let me know if that helps. – Alex Carlos Feb 02 '16 at 17:56
  • Thanks, Alex. The sample app given there works well, but it uses jinja2 instead of django... – LA_ Feb 02 '16 at 19:11