Skip to content

Instantly share code, notes, and snippets.

@vonNiklasson
Last active January 11, 2019 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vonNiklasson/5fec59ad635ff3083f914188cb6736cf to your computer and use it in GitHub Desktop.
Save vonNiklasson/5fec59ad635ff3083f914188cb6736cf to your computer and use it in GitHub Desktop.
Management script for Django to easily run the 'makemessages'-command for all files in your Django application.
'''
translate.py
Management script for Django to easily run the
'makemessages'-command for all files in your Django application.
Put in any registered django app in the location
<app>/management/commands/translate.py
and then use
python manage.py translate
to run makemessages on all files in your project
TODO: Move ignore-configuration to django.conf.settings-file instead
Made by Johan Niklasson
https://github.com/vonNiklasson
https://gist.github.com/vonNiklasson/5fec59ad635ff3083f914188cb6736cf
'''
from django.core.management.base import BaseCommand
from django.core import management
from django.conf import settings
class Command(BaseCommand):
help = "Runs command makemessages for all domains"
def add_arguments(self, parser):
parser.add_argument('--regular',
action='store_true',
dest='regular',
default=False,
help='Makes the translation for python and template files only (excludes JavaScript translation unless stated)')
parser.add_argument('--js',
action='store_true',
dest='js',
default=False,
help='Makes the translation for javascript files only (excludes regular translation unless stated)')
def handle(self, *args, **options):
languages = [ seq[0] for seq in settings.LANGUAGES ];
if options['regular'] == True or (options['regular'] == False and options['js'] == False):
self.stdout.write("Translating Python and template files")
management.call_command('makemessages', locale=languages, domain='django', ignore=['node_modules/*'])
if options['js'] == True or (options['js'] == False and options['regular'] == False):
self.stdout.write("Translating JavaScript files")
management.call_command('makemessages', locale=languages, domain='djangojs', ignore=['node_modules/*'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment