How to override the django admin translation?
Asked Answered
T

2

38

I'm trying to override the default translations of Django's admin site.

I'm using Django 1.6. My settings.py contains:

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

# ...

LANGUAGE_CODE = 'nl'
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)

I have copied the file django/contrib/admin/locale/nl/LC_MESSAGES/django.po to my_project/locale/nl/LC_MESSAGES/django.po and I've made some changes to it.

Next, I have run python manage.py compilemessages and python manage.py runserver.

When I visit localhost:8000/admin, however, I'm still seeing Django's default admin translations. What am I doing wrong?

Edit - I found the problem:

The above description is the correct way to override app translations. I followed my own instructions and they work. The reason for my problem was that I accidentally omitted the nl subdirectory the first time. I am a dumb person.

Technical answered 1/1, 2014 at 22:31 Comment(7)
Did you restart your development server? In my experience, translation files are aggressively cachedKelliekellina
I restarted the development server and I've deployed the code. Still ignoring my translations :-(Technical
Do you mean you deleted the 'nl' subdirectory?Semiannual
side note, django 1.6 is no longer supported for a long time now. You should consider upgrading to 2.2Corr
@hedgie could you write up an answer and accept it yourself to close this question?Coniah
Since you found the solution, you might consider answering your own question as a benefit to other users.Japan
hello, LANGUAGE_CODE = 'nl' is wrong. you should add the full locale nl-NLRend
P
7

I'm providing an answer, even though @hedgie mostly answered their own question. I'll add a bit of context and description of what's happening. This answer is still applicable as of Django 3.0.

Just as you can override a Django-provided admin template by duplicating the template's name and directory structure within our own project, you can override Django-provided admin translations by duplicating a .po file's name and directory structure within our project.

Django's admin translations live in django/contrib/admin/locale/ and are organized by language in directories named [language code]/LC_MESSAGES/. These individual language directories contain two .po files, django.po and djangojs.po, and their respective compiled .mo files. You will be overriding the .po files, and compiling our own .mo files.

The first thing you have to do is enable translations in settings, and tell Django where you store our translation files.

settings.py

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

# ...

LANGUAGE_CODE = 'nl-NL'
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)  # our custom translations will go here

Note that although the directory Django uses is nl, the full language identifier for Dutch in the Netherlands is nl-NL. You can find a full list of identifiers organized by country here.

Next, you'll mimic Django's directory structure and create two files in a new directory:

my_project/locale/nl/LC_MESSAGES/django.po

my_project/locale/nl/LC_MESSAGES/djangojs.po

Note that this path must also match what you provided in settings.py.

Copy and paste the contents of Django's translation files. You can now edit the translations for whichever strings you like. For example:

django.po

msgid "Are you sure?"
--- msgstr "Weet u het zeker?"
+++ msgstr "Weet u het zeker?!"

Now you need to compile the messages with:

python manage.py compilemessages

This command compiles your .po files into .mo files, which Django will use to translate any matching gettext calls. You should now see your custom translations in the admin interface.

Pedigo answered 7/2, 2020 at 17:43 Comment(1)
Awesome! Just a note: No need to Copy and paste the contents of Django's translation files. The new file seems to be merged with the existing .po files.Abixah
F
0

I know this old, In my case, I want to change the 'Authentication and Authorization' app name. This is how I achieved it.

  1. Run the command, python manage.py makemessages -l en

    Note: This command will generate a .po file in the locale/en/LC_MESSAGES/ directory.

    Before running this command, make sure you have set the locale directory path first in your setting.py, mine looks like this: LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)

  2. Open the django.po, delete everything in it, and add these lines,

    #: contrib/auth/apps.py:16
    msgid "Authentication and Authorization"
    msgstr "User Authentication"
    

    Note: In my case, I am renaming the Authentication and Authorization app name of the auth model

  3. Run the command python manage.py compilemessages to compile the translations. Note: This will generate the corresponding .mo file.

  4. Restart your Django

I hope this will help others.

Flameproof answered 19/5, 2023 at 4:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.