|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import shutil |
| 4 | +import urllib |
| 5 | + |
| 6 | +from django.core.management.commands import makemessages |
| 7 | +from django.core.management import call_command |
| 8 | +from django.core.management.utils import popen_wrapper |
| 9 | + |
| 10 | +from base.management import BaseCommand |
| 11 | + |
| 12 | +# This makes makemessages create both translations for Python and JavaScript |
| 13 | +# code in one go. |
| 14 | +# |
| 15 | +# Additionally, it works around xgettext not being able to work with ES2015 |
| 16 | +# template strings [1] by transpiling the JavaScript files in a special way to |
| 17 | +# replace all template strings. |
| 18 | +# |
| 19 | +# [1] https://savannah.gnu.org/bugs/?50920 |
| 20 | + |
| 21 | + |
| 22 | +class Command(makemessages.Command, BaseCommand): |
| 23 | + def handle(self, *args, **options): |
| 24 | + call_command("transpile") |
| 25 | + options["ignore_patterns"] += [ |
| 26 | + "venv", |
| 27 | + ".direnv", |
| 28 | + "node_modules", |
| 29 | + "static-transpile", |
| 30 | + ] |
| 31 | + options["domain"] = "django" |
| 32 | + super().handle(*args, **options) |
| 33 | + options["domain"] = "djangojs" |
| 34 | + self.temp_dir_out = tempfile.mkdtemp() |
| 35 | + self.temp_dir_in = tempfile.mkdtemp() |
| 36 | + super().handle(*args, **options) |
| 37 | + shutil.rmtree(self.temp_dir_in) |
| 38 | + shutil.rmtree(self.temp_dir_out) |
| 39 | + |
| 40 | + def process_locale_dir(self, locale_dir, files): |
| 41 | + if self.domain == "djangojs": |
| 42 | + for file in files: |
| 43 | + # We need to copy the JS files first, as otherwise babel will |
| 44 | + # attempt to read package.json files in subdirs, such as |
| 45 | + # base/package.json |
| 46 | + in_path = urllib.parse.urljoin( |
| 47 | + self.temp_dir_in + "/", file.dirpath |
| 48 | + ) |
| 49 | + os.makedirs(in_path, exist_ok=True) |
| 50 | + in_file = urllib.parse.urljoin(in_path + "/", file.file) |
| 51 | + shutil.copy2(file.path, in_file) |
| 52 | + out_path = urllib.parse.urljoin( |
| 53 | + self.temp_dir_out + "/", file.dirpath |
| 54 | + ) |
| 55 | + file.dirpath = out_path |
| 56 | + os.chdir(".transpile/") |
| 57 | + out, err, status = popen_wrapper( |
| 58 | + [ |
| 59 | + "npm", |
| 60 | + "run", |
| 61 | + "babel-transform-template-literals", |
| 62 | + "--", |
| 63 | + "--out-dir", |
| 64 | + self.temp_dir_out, |
| 65 | + self.temp_dir_in, |
| 66 | + ] |
| 67 | + ) |
| 68 | + os.chdir("../") |
| 69 | + |
| 70 | + super().process_locale_dir(locale_dir, files) |
| 71 | + |
| 72 | + def write_po_file(self, potfile, locale): |
| 73 | + if self.domain == "djangojs": |
| 74 | + with open(potfile, encoding="utf-8") as fp: |
| 75 | + msgs = fp.read() |
| 76 | + # Remove temp dir path info |
| 77 | + msgs = msgs.replace(self.temp_dir_out + "/", "") |
| 78 | + with open(potfile, "w", encoding="utf-8") as fp: |
| 79 | + fp.write(msgs) |
| 80 | + super().write_po_file(potfile, locale) |
0 commit comments