Search code examples
djangoherokuinternationalization

Heroku - Django - Translation not working in production


I am trying to translate some static text with a Django app.

It works on my local, but not on production. I use Heroku.

What I did:

After doing some research it seems to be partly because .mo is in my .gitignore.py.

However, I read a few times that this should remain as such and instead automate compile messages in an app.json file. Which is what I did below:

app.json

{
    "scripts": {
      "release": "python manage.py compilemessages"
    },
    }

procfile

release: python manage.py compilemessages
web: daphne mysite.asgi:application --port $PORT --bind 0.0.0.0 -v2

With the 'gettext' buildpack added to Heroku, I did manage to see a large push of lines contained in my local .mo file on the production logs.

However, I cannot seem to be able to render them.

I can clearly see the .mo files when running:

heroku run bash -a appname 

find . -name "*.mo"

edit##

However whe running 
heroku run bash -a appname 
cd locale/fr/LC_MESSAGES
ls

I only see the below and no .mo file on sight.

~/locale/fr/LC_MESSAGES $ ls
django.po

This makes me think that my script i app.jso is not working properly.

Questions:

  1. Should I simply remove the .mo from .gitignore to fix the issue?

  2. Am I right to continue on the current path and find a solution without removing .mo from .gitignore?


Solution

  • OK so I found the answer.

    The problem is related to Heroku 20 stack.

    My error:

    Instead of having a Procfile that looks like this: Procfile (old)

    release: python manage.py compilemessages
    web: daphne mysite.asgi:application --port $PORT --bind 0.0.0.0 -v2
    

    The solution:

    STEP 1

    I should have left it as is:

    Procfile (now)

    web: daphne mysite.asgi:application --port $PORT --bind 0.0.0.0 -v2
    

    I removed release: python manage.py compilemessages

    STEP 2

    Create a folder called "bin" in my root directory (top level directory, where all folders are visible) and create a file called "post_compile" with no extension.

    Inside this post_compile file, I wrote the following line:

    python manage.py compilemessages
    

    and it worked.

    The answer was explained here:Django Translation on Heroku is not fully working