Last active
April 19, 2022 08:39
-
-
Save philipn/cc873d7311f12eac0f68 to your computer and use it in GitHub Desktop.
Using i18n LANGUAGE_CODE(s) with django-compressor offline compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A replacement for the django-compressor `compress` management command that renders all | |
templates with each `LANGUAGE_CODE` available in your `settings.LANGUAGES`. | |
Useful for making static-i18n work with django-compressor, among other things. | |
""" | |
import os | |
from os.path import join | |
import json | |
from copy import copy | |
from django.core.management.base import BaseCommand | |
from django.core.management import call_command | |
from compressor.conf import settings | |
ORIGINAL_COMPRESS_OFFLINE_CONTEXT = settings.COMPRESS_OFFLINE_CONTEXT | |
ORIGINAL_OFFLINE_MANIFEST = settings.COMPRESS_OFFLINE_MANIFEST | |
class Command(BaseCommand): | |
help = ('Just like `compress`, but iterates through LANGUAGES.') | |
def handle(self, *args, **options): | |
manifest = {} | |
MANIFEST_ROOT = join(settings.STATIC_ROOT, 'CACHE') | |
# Write a bunch of different manifests for each language | |
for code, description in settings.LANGUAGES: | |
COMPRESS_OFFLINE_CONTEXT = copy(ORIGINAL_COMPRESS_OFFLINE_CONTEXT) | |
COMPRESS_OFFLINE_CONTEXT['LANGUAGE_CODE'] = code | |
settings.COMPRESS_OFFLINE_CONTEXT = COMPRESS_OFFLINE_CONTEXT | |
settings.COMPRESS_OFFLINE_MANIFEST = '%s_manifest.json' % code | |
call_command('compress', *args, **options) | |
manifest_for_code = json.load(open(join(MANIFEST_ROOT, '%s_manifest.json' % code))) | |
manifest.update(manifest_for_code) | |
os.remove(join(MANIFEST_ROOT, '%s_manifest.json' % code)) | |
# Combine them all into one manifest | |
f = open(join(MANIFEST_ROOT, ORIGINAL_OFFLINE_MANIFEST), 'w') | |
json.dump(manifest, f) | |
f.close() |
Hi
This is just what I am looking for, but I don't know where to put this file or how to use it ?
Thanks !
Best to use a generator : https://django-compressor.readthedocs.io/en/stable/settings/#offline-settings
thanks. but I have error:
TypeError: Unknown option(s) for compress command: skip_checks.
added del options['skip_checks'] removed error.
`
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--force', action='store_true')
def handle(self, *args, **options):
manifest = {}
MANIFEST_ROOT = join(settings.STATIC_ROOT, 'CACHE')
del options['skip_checks']
# Write a bunch of different manifests for each language
for code, description in settings.LANGUAGES:
COMPRESS_OFFLINE_CONTEXT = copy(ORIGINAL_COMPRESS_OFFLINE_CONTEXT)
COMPRESS_OFFLINE_CONTEXT['LANGUAGE_CODE'] = code
settings.COMPRESS_OFFLINE_CONTEXT = COMPRESS_OFFLINE_CONTEXT
settings.COMPRESS_OFFLINE_MANIFEST = '%s_manifest.json' % code
call_command('compress', *args, **options)
manifest_for_code = json.load(open(join(MANIFEST_ROOT, '%s_manifest.json' % code)))
manifest.update(manifest_for_code)
os.remove(join(MANIFEST_ROOT, '%s_manifest.json' % code))
# Combine them all into one manifest
f = open(join(MANIFEST_ROOT, ORIGINAL_OFFLINE_MANIFEST), 'w')
json.dump(manifest, f)
f.close()
`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works like a charm!
Thank you!