Search code examples
htmldjangotranslationpo

Django Translations Not Working


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>

Solution

  • 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