2020-01-21 06:35:58 +00:00
|
|
|
import glob
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.management import CommandError
|
2022-05-14 17:57:27 +00:00
|
|
|
from django.core.management.commands.makemessages import (
|
|
|
|
Command as MakeMessagesCommand,
|
|
|
|
check_programs,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
from judge.models import NavigationBar, ProblemType
|
|
|
|
|
|
|
|
|
|
|
|
class Command(MakeMessagesCommand):
|
|
|
|
def add_arguments(self, parser):
|
2022-05-14 17:57:27 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--locale",
|
|
|
|
"-l",
|
|
|
|
default=[],
|
|
|
|
dest="locale",
|
|
|
|
action="append",
|
|
|
|
help="Creates or updates the message files for the given locale(s) (e.g. pt_BR). "
|
|
|
|
"Can be used multiple times.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--exclude",
|
|
|
|
"-x",
|
|
|
|
default=[],
|
|
|
|
dest="exclude",
|
|
|
|
action="append",
|
|
|
|
help="Locales to exclude. Default is none. Can be used multiple times.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--all",
|
|
|
|
"-a",
|
|
|
|
action="store_true",
|
|
|
|
dest="all",
|
|
|
|
default=False,
|
|
|
|
help="Updates the message files for all existing locales.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-wrap",
|
|
|
|
action="store_true",
|
|
|
|
dest="no_wrap",
|
|
|
|
default=False,
|
|
|
|
help="Don't break long message lines into several lines.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-obsolete",
|
|
|
|
action="store_true",
|
|
|
|
dest="no_obsolete",
|
|
|
|
default=False,
|
|
|
|
help="Remove obsolete message strings.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--keep-pot",
|
|
|
|
action="store_true",
|
|
|
|
dest="keep_pot",
|
|
|
|
default=False,
|
|
|
|
help="Keep .pot file after making messages. Useful when debugging.",
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2022-05-14 17:57:27 +00:00
|
|
|
locale = options.get("locale")
|
|
|
|
exclude = options.get("exclude")
|
|
|
|
self.domain = "dmoj-user"
|
|
|
|
self.verbosity = options.get("verbosity")
|
|
|
|
process_all = options.get("all")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
# Need to ensure that the i18n framework is enabled
|
|
|
|
if settings.configured:
|
|
|
|
settings.USE_I18N = True
|
|
|
|
else:
|
|
|
|
settings.configure(USE_I18N=True)
|
|
|
|
|
|
|
|
# Avoid messing with mutable class variables
|
2022-05-14 17:57:27 +00:00
|
|
|
if options.get("no_wrap"):
|
|
|
|
self.msgmerge_options = self.msgmerge_options[:] + ["--no-wrap"]
|
|
|
|
self.msguniq_options = self.msguniq_options[:] + ["--no-wrap"]
|
|
|
|
self.msgattrib_options = self.msgattrib_options[:] + ["--no-wrap"]
|
|
|
|
self.xgettext_options = self.xgettext_options[:] + ["--no-wrap"]
|
|
|
|
if options.get("no_location"):
|
|
|
|
self.msgmerge_options = self.msgmerge_options[:] + ["--no-location"]
|
|
|
|
self.msguniq_options = self.msguniq_options[:] + ["--no-location"]
|
|
|
|
self.msgattrib_options = self.msgattrib_options[:] + ["--no-location"]
|
|
|
|
self.xgettext_options = self.xgettext_options[:] + ["--no-location"]
|
|
|
|
|
|
|
|
self.no_obsolete = options.get("no_obsolete")
|
|
|
|
self.keep_pot = options.get("keep_pot")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
if locale is None and not exclude and not process_all:
|
2022-05-14 17:57:27 +00:00
|
|
|
raise CommandError(
|
|
|
|
"Type '%s help %s' for usage information."
|
|
|
|
% (os.path.basename(sys.argv[0]), sys.argv[1])
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
self.invoked_for_django = False
|
|
|
|
self.locale_paths = []
|
|
|
|
self.default_locale_path = None
|
2022-05-14 17:57:27 +00:00
|
|
|
if os.path.isdir(os.path.join("conf", "locale")):
|
|
|
|
self.locale_paths = [os.path.abspath(os.path.join("conf", "locale"))]
|
2020-01-21 06:35:58 +00:00
|
|
|
self.default_locale_path = self.locale_paths[0]
|
|
|
|
self.invoked_for_django = True
|
|
|
|
else:
|
|
|
|
self.locale_paths.extend(settings.LOCALE_PATHS)
|
|
|
|
# Allow to run makemessages inside an app dir
|
2022-05-14 17:57:27 +00:00
|
|
|
if os.path.isdir("locale"):
|
|
|
|
self.locale_paths.append(os.path.abspath("locale"))
|
2020-01-21 06:35:58 +00:00
|
|
|
if self.locale_paths:
|
|
|
|
self.default_locale_path = self.locale_paths[0]
|
|
|
|
if not os.path.exists(self.default_locale_path):
|
|
|
|
os.makedirs(self.default_locale_path)
|
|
|
|
|
|
|
|
# Build locale list
|
2022-05-14 17:57:27 +00:00
|
|
|
locale_dirs = list(
|
|
|
|
filter(os.path.isdir, glob.glob("%s/*" % self.default_locale_path))
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
all_locales = list(map(os.path.basename, locale_dirs))
|
|
|
|
|
|
|
|
# Account for excluded locales
|
|
|
|
if process_all:
|
|
|
|
locales = all_locales
|
|
|
|
else:
|
|
|
|
locales = locale or all_locales
|
|
|
|
locales = set(locales) - set(exclude)
|
|
|
|
|
|
|
|
if locales:
|
2022-05-14 17:57:27 +00:00
|
|
|
check_programs("msguniq", "msgmerge", "msgattrib")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
check_programs("xgettext")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
potfiles = self.build_potfiles()
|
|
|
|
|
|
|
|
# Build po files for each selected locale
|
|
|
|
for locale in locales:
|
|
|
|
if self.verbosity > 0:
|
|
|
|
self.stdout.write("processing locale %s\n" % locale)
|
|
|
|
for potfile in potfiles:
|
|
|
|
self.write_po_file(potfile, locale)
|
|
|
|
finally:
|
|
|
|
if not self.keep_pot:
|
|
|
|
self.remove_potfiles()
|
|
|
|
|
|
|
|
def find_files(self, root):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def _emit_message(self, potfile, string):
|
2022-05-14 17:57:27 +00:00
|
|
|
potfile.write(
|
|
|
|
"""
|
2020-01-21 06:35:58 +00:00
|
|
|
msgid "%s"
|
|
|
|
msgstr ""
|
2022-05-14 17:57:27 +00:00
|
|
|
"""
|
|
|
|
% string.replace("\\", r"\\")
|
|
|
|
.replace("\t", "\\t")
|
|
|
|
.replace("\n", "\\n")
|
|
|
|
.replace('"', '\\"')
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def process_files(self, file_list):
|
2022-05-14 17:57:27 +00:00
|
|
|
with io.open(
|
|
|
|
os.path.join(self.default_locale_path, "dmoj-user.pot"),
|
|
|
|
"w",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as potfile:
|
2020-01-21 06:35:58 +00:00
|
|
|
if self.verbosity > 1:
|
2022-05-14 17:57:27 +00:00
|
|
|
self.stdout.write("processing navigation bar")
|
|
|
|
for label in NavigationBar.objects.values_list("label", flat=True):
|
2020-01-21 06:35:58 +00:00
|
|
|
if self.verbosity > 2:
|
|
|
|
self.stdout.write('processing navigation item label "%s"\n' % label)
|
|
|
|
self._emit_message(potfile, label)
|
|
|
|
|
|
|
|
if self.verbosity > 1:
|
2022-05-14 17:57:27 +00:00
|
|
|
self.stdout.write("processing problem types")
|
|
|
|
for name in ProblemType.objects.values_list("full_name", flat=True):
|
2020-01-21 06:35:58 +00:00
|
|
|
if self.verbosity > 2:
|
|
|
|
self.stdout.write('processing problem type name "%s"\n' % name)
|
|
|
|
self._emit_message(potfile, name)
|