djangodjango-admindjango-management-commandmakemessages

django-admin makemessages --no-obsolete doesn't seem to be working


First of all, I am expecting --no-obsolete would comment out msgid and msgstr if gettext is deleted, right?

How I am testing is:

  1. I wrote gettext("some string here") in view
  2. I ran makemessages command
  3. It wrote a .po file as expected
  4. Then I deleted gettext() line from view and saved file, verified runserver working.
  5. I ran makemessages --no-obsolete and it has not made any changes to .po file.

.po file content extract .

#. Translators: This message is a test of wrap line
#: servers/views.py:31
msgid "Do let me know if it works."
msgstr ""

dev environment

Django = 1.11
OS = Mac/Ubuntu 14.04

settings.py

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOCALE = (
      os.path.join(os.path.dirname(__file__), "locale"),
)

Solution

  • Now with the help of Julien and Tarun, I found following observations.

    python manage.py makemessages -l <locale>

    If there is no gettext in the file being processed, the above command won't write/update .po file. That means if the corresponding .po file earlier had entries for msgstr and msgid, then it won't remove those entries unless file being processed had at least one gettext.

    Note: Above behavior is irrespective of --no-obsolete

    Now to make the --no-obsolete work as expected we need to follow the steps below.

    1. First thing run python manage.py makemessages -l <locale>, this would write .po file with msgid and msgstr.

    2. Now set msgstr and run python manage.py compilemessages -l <locale>. This command writes .mo file in the same directory as .po file.

    3. Now next time when you run makemessages again (without --no-obsolete), .po and .mo files are compared and missing/deleted gettext are commented in .po file.

    4. And when you run makemessages --no-obsolete, commented entries are removed from the .po file.

    E.g

    if you have 3 gettext entries, and you run makemessages first time, it would write 3 msgid and 3 msgstr in .po file. Now if you remove all gettext entries, .po file won't be updated after you run makemessages again, but if your keep at least 1 gettext entry in same file and run makemessages again, it would delete all msgid and msgstr for deleted gettext entries.

    But if you run compilemessages after makemessages, .mo file is created and then for subsequent makemessages commands .po and .mo files are compared and then msgid and msgstr is commented in .po file for deleted gettext entries.

    Then finally when you run makemessages with --no-obsolete option the commented messages from .po files are deleted permanently.