1

I am working in a Django 1.9 / python3.5 application, and trying to make use of Django's translation utility. I have a locale directory which has an 'es' directory for spanish translations that I created a .po file in. I set it up to have a couple translations just to test it out.

msgid "Sign In"
msgstr "Registrarse"

msgid "Create an Account"
msgstr "Crea una cuenta"

I have my setting file correctly configured as well

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'webapp.middleware.LanguageSwitchMiddleware',
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'webapp.context_processors.detail_context',
                'django.template.context_processors.i18n'
            ],
        },
    },
]

# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/
LOCALE_PATHS = (
    os.path.join(PROJECT_ROOT, 'locale/'),
)

from django.utils.translation import ugettext_lazy as _

LANGUAGES = (
    ('en', _('English')),  # first language is the default used by modeltranslations
    ('es', _('Spanish')),
)

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Chicago'

USE_I18N = True

In my template I use the Django 'trans' template tag for the words sign in, and create an account. A select box will edit the Content-Language http response header from the application, which I have tested, and it successfully does so. However the headers sign up, and create and account, do not translate to Spanish. Is there some step I'm missing ?

HTML

{% load i18n %}

<ul class="list-inline-xxs">

    {% if customer %}
        <li>
            Welcome,
            <a href='{% url "customer:dashboard" %}'>
                {{ customer.first_name }}
            </a>
        </li>
        <li>
            <a href='{% url "customer:logout" %}'>
                {% trans 'Logout' %}
            </a>
        </li>
    {% else %}
        <li>
            <a href='{% url "customer:login" %}'>
                {% trans 'Sign In' %}
            </a>
        </li>
        <li>
            <a href='{% url "subscription:customer-subscribe" %}'>
                {% trans 'Create an Account' %}
            </a>
        </li>
    {% endif %}

</ul>
TJB
  • 3,706
  • 9
  • 51
  • 102

1 Answers1

3

I have a locale directory which has an 'es' directory for spanish translations that I created a .po file in.

That ^^^ line suggests you are creating translation files manually. Let Django create translation files for you:

django-admin makemessages -a

Then put in your translations, save the file and compile with

django-admin compilemessages

Restart your app and it should work.

I've put together a simple example how to do translations with Django: https://github.com/DusanMadar/Django-multilang-demo

EDIT

As django django-admin makemessages -h suggests --ignore PATTERN, -i PATTERN is what you need to use to ignore third party directories. So something like django-admin makemessages -a -i 3rdparty_dir

Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
  • I did not, but I just added it to the template configuration, and it still doesn't work : ( . I will update the question with my templates and middleware config. – TJB Nov 02 '17 at 18:32
  • Ahh I see. It may be because I didn't compile them to get an .mo file. When i run the django admin command to compile the messages, I get this error: Can't find msgfmt. Make sure you have GNU gettext tools 0.15 or newer installed. – TJB Nov 02 '17 at 19:07
  • you need to install `gettext`: `brew install gettext` or `apt install gettext` or even something different depending on your system. – Dušan Maďar Nov 02 '17 at 19:09
  • Is there a way to just run compilemessages on one locale directory ? When I run it from the root of my app it goes through all my third party directories that have locale files and recreates .mo files for those as well. The problem is that this causes a "plural forms expression could be dangerous" value error because it is re-setting the plural-forms variable in some .po files. – TJB Nov 02 '17 at 19:51