Skip to content

Instantly share code, notes, and snippets.

@charlwillia6
Forked from vonNiklasson/translate.py
Last active May 26, 2023 02:21
Show Gist options
  • Save charlwillia6/9b1e311dd6747c867631e6cde1c527aa to your computer and use it in GitHub Desktop.
Save charlwillia6/9b1e311dd6747c867631e6cde1c527aa 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. #django #translations
'''
translate.py
Management script for Django to easily run the
'makemessages'-command and 'compilemessages'-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
Made by Johan Niklasson
https://github.com/vonNiklasson
https://gist.github.com/vonNiklasson/5fec59ad635ff3083f914188cb6736cf
Modified by Charles Williams
https://github.com/charlwillia6
https://gist.github.com/charlwillia6/9b1e311dd6747c867631e6cde1c527aa
Add LANGUAGE_IGNORE_PATTERNS = [] to you settings file for custom ignore
patterns
'''
from django.core.management.base import BaseCommand
from django.core import management
class Command(BaseCommand):
settings = None
ignore_patterns = ['node_modules/*']
help = "Runs command makemessages for all domains"
def add_arguments(self, parser):
parser.add_argument(
'--make-django',
action='store_true',
dest='make-django',
default=False,
help='Makes the translation for python and template files only (excludes JavaScript translation unless stated)'
)
parser.add_argument(
'--make-djangojs',
action='store_true',
dest='make-djangojs',
default=False,
help='Makes the translation for javascript files only (excludes regular translation unless stated)'
)
parser.add_argument(
'--compile-all',
action='store_true',
dest='compile-all',
default=False,
help='Compiles .po files to .mo files (compilemessages only)'
)
def handle(self, *args, **options):
if self.settings is None:
raise Exception("settings is None")
if hasattr(self.settings, 'LANGUAGE_IGNORE_PATTERNS'):
ignore_patterns = self.settings.LANGUAGE_IGNORE_PATTERNS
else:
ignore_patterns = Command.ignore_patterns
languages = [seq[0] for seq in self.settings.LANGUAGES]
if options['compile-all'] == False:
if options['make-django'] == True or (options['make-django'] == False and options['make-djangojs'] == False):
self.stdout.write("Translating Python and template files")
management.call_command('makemessages', locale=languages, domain='django', ignore=ignore_patterns)
if options['make-djangojs'] == True or (options['make-djangojs'] == False and options['make-django'] == False):
self.stdout.write("Translating JavaScript files")
management.call_command('makemessages', locale=languages, domain='djangojs', ignore=ignore_patterns)
else:
self.stdout.write("You cannot use the --compile-all and --make-[django/djangjs] arguments together")
if options['compile-all'] == True and options['make-django'] == False and options['make-djangojs'] == False:
self.stdout.write("Compiling All Translation Files")
management.call_command('compilemessages', local=languages)
@vonNiklasson
Copy link

Cheers, great work on the script :) I haven't been working with any Django project for a while, but you've made some great changes! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment