Localization: django-admin compilemessages skip venv
Asked Answered
F

5

16

I am using localization in Django 1.11 application. I can exclude the virtual environment folder and node_modules folder while adding the messages in message file using -i option like:

django-admin makemessages -l 'no' -i venv
django-admin makemessages -d djangojs --locale no -i venv -i node_modules

After adding the translations I am compiling messages using:

django-admin compilemessages

It processes django.po files of all installed packages located in virtual environment folder. Thus it takes longer time to finish compiling translations.

I did not find any argument parameter to skip a specific path from compilemessages command in documentation.

Is there any option to skip the venv or specific path from compilemessages?

Folio answered 17/1, 2019 at 4:31 Comment(2)
I have the same question, and am surprised the ignore flag is possible for makemessages and not compilemessages. Have you found any solution in the meantime? This thread recommends moving the venv folder out of the django project, but the possibility to ignore it in the compilemessages command would be preferable.Irresolution
No. I have not found any solution yet.Folio
V
20

Django 3.0 added --ignore option

django-admin compilemessages --ignore=cache --ignore=outdated/*/locale

Docs: https://docs.djangoproject.com/en/3.2/ref/django-admin/#cmdoption-compilemessages-ignore


(Method #2)

BEFORE THAT THE BEST HACK I FOUND TO IGNORE THE VENV IS:

cd to project
python ../manage.py makemessages  (jumping one directory up)
python ../manage.py compilemessages

(This little hack from a workmate avoids compiling the venv .po)


(Method #3)

And also before, another workaround was trying the more complicated way of using the --exclude flag

usage: django-admin compilemessages [-h] [--version] [-v {0,1,2,3}]
                                    [--settings SETTINGS]
                                    [--pythonpath PYTHONPATH] [--traceback]
                                    [--no-color] [--locale LOCALE]
                                    [--exclude EXCLUDE] [--use-fuzzy]

github

        parser.add_argument(
            '--exclude', '-x', action='append', default=[],
            help='Locales to exclude. Default is none. Can be used multiple times.',
        )

Unfortunately this is for locales, but it is the only thing I found so far

From these internal communications on the development of Django, I can see the ignore flag has been copied from makemessages to compilemessages for a future version

For my own usage I used (excluding es and en)

django-admin compilemessages --exclude=sw --exclude=sl --exclude=sk --exclude=km --exclude=sv --exclude=ko --exclude=sq --exclude=sr --exclude=kk --exclude=ka --exclude=es_MX --exclude=fa --exclude=fy --exclude=fr --exclude=en_AU --exclude=ne --exclude=nb --exclude=nn --exclude=nl --exclude=id --exclude=az --exclude=io --exclude=ar --exclude=ia --exclude=kn --exclude=it --exclude=is --exclude=vi --exclude=af --exclude=my --exclude=mr --exclude=uk --exclude=pl --exclude=ur --exclude=mk --exclude=mn --exclude=ml --exclude=he --exclude=hi --exclude=hu --exclude=hr --exclude=en_GB --exclude=pa --exclude=cs --exclude=fi --exclude=cy --exclude=sr_Latn --exclude=os --exclude=pt --exclude=ja --exclude=bs --exclude=br --exclude=bn --exclude=ast --exclude=bg --exclude=hsb --exclude=dsb --exclude=ro --exclude=es_CO --exclude=ru --exclude=et --exclude=eu --exclude=zh_Hant --exclude=zh_Hans --exclude=be --exclude=eo --exclude=el --exclude=da --exclude=de --exclude=pt_BR --exclude=ta --exclude=ca --exclude=te --exclude=es_AR --exclude=th --exclude=lt --exclude=lv --exclude=tr --exclude=tt --exclude=es_VE --exclude=lb --exclude=gl --exclude=ga --exclude=gd --exclude=udm--exclude=sw --exclude=sl --exclude=sk --exclude=km --exclude=sv --exclude=ko --exclude=sq --exclude=sr --exclude=kk --exclude=ka --exclude=es_MX --exclude=fa --exclude=fy --exclude=fr --exclude=en_AU --exclude=ne --exclude=nb --exclude=nn --exclude=nl --exclude=id --exclude=az --exclude=io --exclude=ar --exclude=ia --exclude=kn --exclude=it --exclude=is --exclude=vi --exclude=af --exclude=my --exclude=mr --exclude=uk --exclude=pl --exclude=ur --exclude=mk --exclude=mn --exclude=ml --exclude=he --exclude=hi --exclude=hu --exclude=hr --exclude=en_GB --exclude=pa --exclude=cs --exclude=fi --exclude=cy --exclude=sr_Latn --exclude=os --exclude=pt --exclude=ja --exclude=bs --exclude=br --exclude=bn --exclude=ast --exclude=bg --exclude=hsb --exclude=dsb --exclude=ro --exclude=es_CO --exclude=ru --exclude=et --exclude=eu --exclude=zh_Hant --exclude=zh_Hans --exclude=be --exclude=eo --exclude=el --exclude=da --exclude=de --exclude=pt_BR --exclude=ta --exclude=ca --exclude=te --exclude=es_AR --exclude=th --exclude=lt --exclude=lv --exclude=tr --exclude=tt --exclude=es_VE --exclude=lb --exclude=gl --exclude=ga --exclude=gd --exclude=udm --exclude=zh_CN  --exclude=ky --exclude=zh_TW --exclude=no --exclude=pt_PT  --exclude=hy
Varityper answered 7/5, 2019 at 13:51 Comment(2)
If you're getting the error: "CommandError: This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified." , you might need to set LOCALE_PATHS in your settings.Tiphany
Github link brokenGiant
I
8

As others already stated, in Django 2.x sadly there are only hacks to deal with this. (Django 3.0 finally added the --ignore to compilemessages.)

The one I found to be most transparent was to debug into compilemessages and look at the subprocess calls it issues. From that you can derive the direct calls to the msgfmt tool.

For our comparably simple project, makemessages collects the *.po files in locale/$LANGUAGE/LC_MESSAGES/django.po. Then msgfmt would put the generated *.mo in the same folder. So we just wrote a script to perform steps like this:

set -e

django-admin makemessages --all --ignore venv

# HACK: Run msgfmt manually instead from "django-admin compilemessages"
# because the latter also searches venv.
msgfmt -o locale/de/LC_MESSAGES/django.mo locale/de/LC_MESSAGES/django.po
msgfmt -o locale/en/LC_MESSAGES/django.mo locale/en/LC_MESSAGES/django.po
msgfmt -o locale/hu/LC_MESSAGES/django.mo locale/hu/LC_MESSAGES/django.po
# ...add other languages as needed.

This is of course incredibly clumsy but easy to understand and extend.

Illsorted answered 8/8, 2019 at 12:7 Comment(0)
W
5

Django 3.0 added --ignore option

django-admin compilemessages --ignore=cache --ignore=outdated/*/locale

Docs: https://docs.djangoproject.com/en/3.1/ref/django-admin/#cmdoption-compilemessages-ignore

Woody answered 18/8, 2020 at 11:58 Comment(0)
S
5

Using ignore option

python manage.py compilemessages -i "venv*"

this command worked for me. Make sure, venv should be in double quotes(""), not single quote('')

Spermatophore answered 29/9, 2020 at 10:25 Comment(1)
If you are using docker, make sure the path is relative to the pwd, when I tried ./manage.py compilemessages -i "/app/.tox*" it didn't work, I needed to use ./manage.py compilemessages -i ".tox*"Shelled
M
0

If you are running this command manually from python, here is the correct syntax for ignoring the virtualenv:

management.call_command("compilemessages", ignore_patterns=[".venv/*"])
Mindy answered 1/3, 2022 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.