Msonic
Msonic

Reputation: 1456

Setting up translation in Django - Makemessages doesn't create a file

I'm currently trying to setup my website for translation. My website is currently in French and I would like to translate it in English. Here's what I've done so far:

  1. Put the {% load i18n %} tag at the top of the html
  2. Put a field up for translation with {% trans "Services" %}
  3. In settings.py:

I've added 'django.middleware.locale.LocaleMiddleware' to the middleware classes list.

I've also set the current language to french with LANGUAGE_CODE = 'fr'

I've specified which languages are available with

ugettext = lambda s: s
LANGUAGES = (
    ('en', ugettext('Anglais')),
    ('fr', ugettext('Français')),
)

I've turned on I18N translation with USE_I18N = True

I've specified where I want the localization files to be

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale/'),
)

When I run django-admin.py makemessages -l en -v3, here's what I get:

PS C:\Users\USERNAME\Desktop\Ajitech\ajitech-website> django-admin.py makemessages -l en -v3
examining files with the extensions: .txt and .html
ignoring directory .git
ignoring file settings.cpython-34.pyc in .\AjitechRoot\__pycache__
ignoring file urls.cpython-34.pyc in .\AjitechRoot\__pycache__
ignoring file wsgi.cpython-34.pyc in .\AjitechRoot\__pycache__
ignoring file __init__.cpython-34.pyc in .\AjitechRoot\__pycache__
ignoring file __init__.cpython-34.pyc in .\AjitechWebsite\migrations\__pycache__
ignoring file admin.cpython-34.pyc in .\AjitechWebsite\__pycache__
ignoring file models.cpython-34.pyc in .\AjitechWebsite\__pycache__
ignoring file views.cpython-34.pyc in .\AjitechWebsite\__pycache__
ignoring file __init__.cpython-34.pyc in .\AjitechWebsite\__pycache__
processing file AjitechData.sqlite3 in .
processing file __init__.py in .\AjitechRoot
processing file settings.py in .\AjitechRoot
processing file urls.py in .\AjitechRoot
processing file wsgi.py in .\AjitechRoot
processing file __init__.py in .\AjitechWebsite
processing file admin.py in .\AjitechWebsite
processing file __init__.py in .\AjitechWebsite\migrations
processing file models.py in .\AjitechWebsite
processing file tests.py in .\AjitechWebsite
processing file views.py in .\AjitechWebsite
processing file Runserver.ps1 in .
processing file db.sqlite3 in .
processing file manage.py in .
processing file base.css in .\static\static-only\admin\css
processing file changelists.css in .\static\static-only\admin\css
processing file dashboard.css in .\static\static-only\admin\css
processing file forms.css in .\static\static-only\admin\css
processing file ie.css in .\static\static-only\admin\css
processing file login.css in .\static\static-only\admin\css
processing file rtl.css in .\static\static-only\admin\css
processing file widgets.css in .\static\static-only\admin\css
processing file changelist-bg.gif in .\static\static-only\admin\img
processing file changelist-bg_rtl.gif in .\static\static-only\admin\img
processing file default-bg-reverse.gif in .\static\static-only\admin\img
processing file default-bg.gif in .\static\static-only\admin\img
processing file deleted-overlay.gif in .\static\static-only\admin\img
processing file move_vertex_off.png in .\static\static-only\admin\img\gis
processing file move_vertex_on.png in .\static\static-only\admin\img\gis
processing file icon-no.gif in .\static\static-only\admin\img
processing file icon-unknown.gif in .\static\static-only\admin\img
processing file icon-yes.gif in .\static\static-only\admin\img
processing file icon_addlink.gif in .\static\static-only\admin\img
processing file icon_alert.gif in .\static\static-only\admin\img
processing file icon_calendar.gif in .\static\static-only\admin\img
processing file icon_changelink.gif in .\static\static-only\admin\img
processing file icon_clock.gif in .\static\static-only\admin\img
processing file icon_deletelink.gif in .\static\static-only\admin\img
processing file icon_error.gif in .\static\static-only\admin\img
processing file icon_searchbox.png in .\static\static-only\admin\img
processing file icon_success.gif in .\static\static-only\admin\img
processing file inline-delete-8bit.png in .\static\static-only\admin\img
processing file inline-delete.png in .\static\static-only\admin\img
processing file inline-restore-8bit.png in .\static\static-only\admin\img
processing file inline-restore.png in .\static\static-only\admin\img
processing file inline-splitter-bg.gif in .\static\static-only\admin\img
processing file nav-bg-grabber.gif in .\static\static-only\admin\img
processing file nav-bg-reverse.gif in .\static\static-only\admin\img
processing file nav-bg-selected.gif in .\static\static-only\admin\img
processing file nav-bg.gif in .\static\static-only\admin\img
processing file selector-icons.gif in .\static\static-only\admin\img
processing file selector-search.gif in .\static\static-only\admin\img
processing file sorting-icons.gif in .\static\static-only\admin\img
processing file tooltag-add.png in .\static\static-only\admin\img
processing file tooltag-arrowright.png in .\static\static-only\admin\img
processing file LICENSE-JQUERY.txt in .\static\static-only\admin\js
processing file SelectBox.js in .\static\static-only\admin\js
processing file SelectFilter2.js in .\static\static-only\admin\js
processing file actions.js in .\static\static-only\admin\js
processing file actions.min.js in .\static\static-only\admin\js
processing file DateTimeShortcuts.js in .\static\static-only\admin\js\admin
processing file RelatedObjectLookups.js in .\static\static-only\admin\js\admin
processing file calendar.js in .\static\static-only\admin\js
processing file collapse.js in .\static\static-only\admin\js
processing file collapse.min.js in .\static\static-only\admin\js
processing file core.js in .\static\static-only\admin\js
processing file inlines.js in .\static\static-only\admin\js
processing file inlines.min.js in .\static\static-only\admin\js
processing file jquery.init.js in .\static\static-only\admin\js
processing file jquery.js in .\static\static-only\admin\js
processing file jquery.min.js in .\static\static-only\admin\js
processing file prepopulate.js in .\static\static-only\admin\js
processing file prepopulate.min.js in .\static\static-only\admin\js
processing file timeparse.js in .\static\static-only\admin\js
processing file urlify.js in .\static\static-only\admin\js
processing file animation.css in .\static\static\css
processing file bootstrap.min.css in .\static\static\css
processing file font-awesome.min.css in .\static\static\css
processing file FontAwesome.otf in .\static\static\fonts
processing file fontawesome-webfont.eot in .\static\static\fonts
processing file fontawesome-webfont.svg in .\static\static\fonts
processing file fontawesome-webfont.ttf in .\static\static\fonts
processing file fontawesome-webfont.woff in .\static\static\fonts
processing file fontawesome-webfont.woff2 in .\static\static\fonts
processing file ajitech-jumbotron-background-color-small.png in .\static\static\images
processing file ajitech-jumbotron-background-small.png in .\static\static\images
processing file ajitech-jumbotron-background.png in .\static\static\images
processing file ajitech-jumbotron-even.png in .\static\static\images
processing file ajitech-jumbotron-odd.png in .\static\static\images
processing file ajitech-logo-large.png in .\static\static\images
processing file ajitech-logo.png in .\static\static\images
processing file ajitech-logo.svg in .\static\static\images
processing file programming.jpg in .\static\static\images\services
processing file username-normal.png in .\static\static\images\team
processing file ajitech.js in .\static\static\js
processing file bootstrap.min.js in .\static\static\js
processing file jquery-2.1.3.min.js in .\static\static\js
processing file less-2.5.0.min.js in .\static\static\js
processing file wow.min.js in .\static\static\js
processing file ajitech-404.less in .\static\static\less
processing file ajitech-utilities.less in .\static\static\less
processing file ajitech.less in .\static\static\less
processing file 404.html in .\static\templates
processing file overview.html in .\static\templates
processing locale en

My message to translate is in overview.html, which has been processed by the script (last file processed).

After all this, I expect a .po file to be generated in the locale folder, containing the "Service" line to be translated to English. However, that is not the case. Is there something I'm missing?


Here's my settings.py

"""
Django settings for AjitechRoot project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '** REDACTED **'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = DEBUG

ALLOWED_HOSTS = [ '127.0.0.1' ]


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'AjitechWebsite',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    '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',
    'django.middleware.locale.LocaleMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.i18n',
)

ROOT_URLCONF = 'AjitechRoot.urls'

WSGI_APPLICATION = 'AjitechRoot.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'AjitechData.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'fr'
TIME_ZONE = 'Eastern/Canada'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale/'),
)

ugettext = lambda s: s
LANGUAGES = (
    ('en', ugettext('Anglais')),
    ('fr', ugettext('Français')),
)


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

# Template location
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'static/templates'),
)

MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/static-only')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static/static'),
)

EDIT:

I'm using Django 1.8.1.

The makemessages command is called from the root folder of my website, the one where you can find manage.py.

Upvotes: 1

Views: 3352

Answers (2)

Msonic
Msonic

Reputation: 1456

As it turned out, I was running a deprecated version of GNU GetText (0.12 to be precise), and makemessages requires version 0.15 or later, as described in the documentation. I've updated the tool and everything works.

Upvotes: 2

mikeser
mikeser

Reputation: 86

According to the documentation, the class LocaleMiddleware has to be located after SessionMiddleware and before CommonMiddleware in the variable MIDDLEWARE_CLASSES. For example:

MIDDLEWARE_CLASSES = (
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.locale.LocaleMiddleware',
   'django.middleware.common.CommonMiddleware',
)

Moreover, at least in Django 1.8 I had to create manually the locale directory inside every app I wanted to get translated.

You should also use the function ugettext_lazy() in your settings.py file:

from django.utils.translation import ugettext_lazy as _

LANGUAGES = (
    ('de', _('German')),
    ('en', _('English')),
)

Hope this helps.

EDIT:

Built-in template context processors were moved from django.core.context_processors to django.template.context_processors in Django 1.8, so you should use:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.template.context_processors.i18n',
)

However, it is not necessary to override the default settings because the i18n context processor is included by default.

Take into account that TEMPLATE_CONTEXT_PROCESSORS is deprecated since Django 1.8.

Upvotes: 1

Related Questions