[Fixed]-Django translations does not work

28👍

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 !

👤jeff

9👍

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.

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    #'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

7👍

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

LANGUAGE_CODE = 'fa-IR'

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

makemessages -l fa_IR

6👍

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.

2👍

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

C:.
β”œβ”€β”€β”€documents
β”œβ”€β”€β”€project
β”‚   β”œβ”€β”€β”€apps
β”‚   β”‚   β”œβ”€β”€β”€accounts
β”‚   β”‚   β”‚   β”œβ”€β”€β”€...
β”‚   β”‚   β”œβ”€β”€β”€common
β”‚   β”‚   β”‚   β”œβ”€β”€β”€...
β”‚   β”‚   └───...
β”‚   β”œβ”€β”€β”€locale
β”‚   β”‚   └───it
β”‚   β”‚       └───LC_MESSAGES
β”‚   └───...
└───tests

The settings file is set in this way:

LANGUAGE_CODE = 'it'

LOCALE_PATHS = [
    os.path.join(Path(__file__).resolve().parent, "locale"),
]

LANGUAGES = [
   ('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.

👤J_Zar

1👍

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.

👤MAA

0👍

For me commenting en line works OK

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

no locale folder in a project root, no custom LOCALE_PATHS

👤slav

0👍

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

👤toxic

0👍

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

TLDR;

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

LANGUAGES = (
    ('hi', _('Hindi')),
    ('en', _('English')),
)
LANGUAGE_CODE = 'hi'

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'
translation.activate(user_language)
response = HttpResponse(...)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, user_language)

I took this from here Explicitly setting the active language

0👍

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.

0👍

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