Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 936
LA_ Avatar asked Jan 24 '16 18:01

LA_


People also ask

What is Gettext_lazy in Django?

gettext_lazy is a callable within the django. utils. translation module of the Django project.

Which among the given below internationalization function in Django marks a string as a translation string without actually translating it at that moment?

Lazy Translation utils. translation. ugettext_lazy() to translate strings lazily – when the value is accessed rather than when the ugettext_lazy() function is called. In this example, ugettext_lazy() stores a lazy reference to the string – not the actual translation.


1 Answers

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 %}
like image 179
Alex Carlos Avatar answered Oct 15 '22 08:10

Alex Carlos