use msgmerge instead of babel for updating PO files (#5292)

use msgmerge instead of babel for updating PO files
This commit is contained in:
Ryan Johnson 2022-11-08 15:20:12 -08:00 коммит произвёл GitHub
Родитель 7bd3edf9fd
Коммит 99c4c2bf5c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 50 добавлений и 45 удалений

Просмотреть файл

@ -1,17 +1,13 @@
from pathlib import Path
import subprocess
import textwrap
from babel import Locale, UnknownLocaleError
from babel.messages.frontend import CommandLineInterface
from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.management.base import BaseCommand, CommandError
def is_supported_for_init(locale):
try:
Locale.parse(locale)
except UnknownLocaleError:
return False
return True
INDENT = " " * 3
CHECKMARK = "\u2713"
class Command(BaseCommand):
@ -31,42 +27,51 @@ class Command(BaseCommand):
if locale in ("en_US", "xx"):
continue
if Path(f"locale/{locale}").is_dir():
sub_cmd = "update"
elif not is_supported_for_init(locale):
# NOTE: Babel only supports initializing locales included in the CLDR.
self.stdout.write(
self.style.WARNING(f"WARNING: skipping locale {locale} (unsupported for init)")
)
continue
else:
sub_cmd = "init"
for domain in ("django", "djangojs"):
CommandLineInterface().run(
[
"pybabel",
sub_cmd,
"-d",
"locale/",
"-l",
locale,
"-D",
"django",
"-i",
"locale/templates/LC_MESSAGES/django.pot",
]
domain_pot = Path(f"locale/templates/LC_MESSAGES/{domain}.pot")
if not domain_pot.is_file():
raise CommandError(f"unable to find {domain_pot}")
domain_po = Path(f"locale/{locale}/LC_MESSAGES/{domain}.po")
if not domain_po.parent.is_dir():
domain_po.parent.mkdir(parents=True)
if not domain_po.is_file():
self.run(
f"initializing {domain_po}",
"msginit",
"--no-translator",
f"--locale={locale}",
f"--input={domain_pot}",
f"--output-file={domain_po}",
"--width=200",
)
self.run(
f"updating {domain_po}",
"msgmerge",
"--update",
"--width=200",
"--backup=off",
domain_po,
domain_pot,
)
def run(self, description, *cmd_and_args):
self.stdout.write(f"{description}...", ending="")
try:
subprocess.run(
cmd_and_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
text=True,
)
CommandLineInterface().run(
[
"pybabel",
sub_cmd,
"-d",
"locale/",
"-l",
locale,
"-D",
"djangojs",
"-i",
"locale/templates/LC_MESSAGES/djangojs.pot",
]
except subprocess.CalledProcessError as err:
self.stdout.write(self.style.ERROR("x"))
self.stdout.write(
self.style.ERROR(textwrap.indent(f"error(s) while {description}:", INDENT))
)
self.stdout.write(self.style.ERROR(textwrap.indent(err.stdout, INDENT * 2)), ending="")
else:
self.stdout.write(self.style.SUCCESS(CHECKMARK))