[Fixed]-Django translations does not work


I solved my issue. In my case, the problem was with the LOCALE_PATHS definition in settings.py.

I tested it in the view by :

from TranslationTest import settings
return HttpResponse(settings.LOCALE_PATHS)

It was showing home/myProjects/TranslationTest/TranslationTest/locale, however makemessages was producing the files in home/myProjects/TranslationTest/locale

so I changed my settings as follows :

SITE_ROOT = os.path.dirname(os.path.realpath(__name__))
LOCALE_PATHS = ( os.path.join(SITE_ROOT, 'locale'), )

and now it works.

But I still wonder, why didn’t makemessages understand that it should create the files in the LOCALE_PATHS specificed by settings.py?

In my understanding, it always produces the locale files in SITE_ROOT/locale, so we should always set LOCALE_PATHS to this? If this is a default, why set it at all? I would appreciate further information on this issue.

Thanks for all the help !



This happens if you have the locale middleware activated. By so doing, I think your session local depends on your browser locale.

if you comment the locale middleware, it should be working as expected.



and keep in mind if your language has a country in it e.g. fa-IR, in your settings file you must add:


but when you want to call makemessages command you should change - to _:

makemessages -l fa_IR


I tried almost all of the offered solutions above without any success. Then I realised that I forgot to run compilemessages after makemessages command. So make sure you run:

python manage.py compilemessages 

After running makemessages. What the compilemessages command does is it converts .po files to .mo files in your locale directories. This is needed because .mo files are the only files that gettext can understand. Without .mo files translation won’t work, which was the case for me.


As of today, with Django 3.2 this issue is still present.
In my case, I have the following setup:

β”‚   β”œβ”€β”€β”€apps
β”‚   β”‚   β”œβ”€β”€β”€accounts
β”‚   β”‚   β”‚   β”œβ”€β”€β”€...
β”‚   β”‚   β”œβ”€β”€β”€common
β”‚   β”‚   β”‚   β”œβ”€β”€β”€...
β”‚   β”‚   └───...
β”‚   β”œβ”€β”€β”€locale
β”‚   β”‚   └───it
β”‚   β”‚       └───LC_MESSAGES
β”‚   └───...

The settings file is set in this way:


    os.path.join(Path(__file__).resolve().parent, "locale"),

   ('en', _('English')),
   ('it', _('Italian')),

The trick is in the definition of LOCALE_PATHS. Django-admin is in some way not synced with the Django environment.



I just want to remind something that cause me a tiny problem and it was that I should change language of browser(from options).
However I did everything completely but I couldn’t see the translated texts.
And by the way I didn’t want to use any specific url for seeing the translation.



For me commenting en line works OK

USE_I18N = True
USE_L10N = True
('uk', _('Ukrainian')),
# ('en', _('English')),

no locale folder in a project root, no custom LOCALE_PATHS



adding the local middle ware to the django middle ware worked for me 'django.middleware.locale.LocaleMiddleware',



You might need to read this How Django discovers language preference.


The locale middleware decides the user language in the following order:

  1. i18n_patterns function in your root URLconf
  2. Failing that, it looks for a cookie set by the LANGUAGE_COOKIE_NAME setting
  3. Failing that, it looks at the Accept-Language HTTP header sent by your Browser
  4. Failing that, it uses the global LANGUAGE_CODE setting

In my case I followed a tutorial and I didn’t had a clue what why my language was not changing.

I was already done with the locale folder where i had all the strings for both of languages translated and i had already setup the LOCALE_PATH and other stuffs correctly.

So for testing i changed the LANGUAGE_CODE to display the text in hindi but i was not aware of the language precedence stuff.

i had set the following in my settings

    ('hi', _('Hindi')),
    ('en', _('English')),

So what was happening was that i had no cookie that specified the language so the precedence fell for the browsers default language passed in http header.

So it never came to the LANGUAGE_CODE in my settings and the language in the request.LANGUAGE_CODE was set to en.

So for solving this issue i set the cookie to take precedence before my browser

from django.conf import settings
from django.http import HttpResponse
from django.utils import translation
user_language = 'hi'
response = HttpResponse(...)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, user_language)

I took this from here Explicitly setting the active language


you can try Candy Translate instead of Django native translations module. It is much easier, do not require to set up the middleware and basically does most of the work for you.
It also allows you to manage the translations in the excel file which comes handy when you are sending the texts to the translators.

It comes with some limitations but for all my websites it was just enough and achieved much faster.


You need to put the JavaScript code in a file inside the static folder(you need to run the collectstatic command) in order to the djangojs.po to be generated. Because Django by default look at this destination when dealing with JS files.

Leave a comment