mincom
mincom

Reputation: 979

Django i18n translations not working in production (Heroku)

My translations are working locally, but in production at Heroku, my site remains in its default language (English) after changing the language.

These are in my settings.py file:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

USE_I18N = True
USE_L10N = True

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

This is my structure:

myproject
├── Procfile
├── locale
│   └── fr
│       └── LC_MESSAGES
│           ├── django.mo
│           └── django.po
├── myproject
│   ├── __init__.py
│   └── settings.py

I thought it was a path issue so I SSH'd into my Heroku app and printed LOCALE_PATHS:

>>> from myproject.settings import LOCALE_PATHS
>>> print(LOCALE_PATHS)
['/app/locale']

And pwd in locale/ returns pwd /app/locale.

What did I do wrong?

Upvotes: 0

Views: 721

Answers (2)

Hexwell
Hexwell

Reputation: 150

The problem is that .mo files (compiled translations) are not present in the repo, and therefore not packaged to be deployed together with the rest of the application during the Heroku build process.

The possible solutions are to:

  1. Add them to the repository
  2. Generate .mo files during the build

I suggest to generate them during the build, for these reasons:

  • It automates compilation, one less manual step
  • It will ensure that the translations are always up to date
  • The compiled files are not source, and therefore should be in the repository

To generate them, you can use the post compile hook of the Heroku build:

Create a file bin/post_compile (no extension, like procfile) with the following line:

python ./manage.py compilemessages

Optionally you can add the specific language (e.g. python ./manage.py compilemessages -l nl)

I got this last part from this answer to a similar question.

Upvotes: 0

mincom
mincom

Reputation: 979

I found the issue:
my django.mo file was ignored by .gitignore as I use the default GitHub Python gitignore file.

Upvotes: 4

Related Questions