[damned-lies/gettext: 1/3] refactor: rewrite update-trans to output code only po
- From: Guillaume Bernard <gbernard src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [damned-lies/gettext: 1/3] refactor: rewrite update-trans to output code only po
- Date: Fri, 25 Jun 2021 06:52:07 +0000 (UTC)
commit 63fb0b3124f018c6264579517d1224679bdbb22a
Author: Guillaume Bernard <associations guillaume-bernard fr>
Date: Sun May 2 16:11:34 2021 +0200
refactor: rewrite update-trans to output code only po
the update-trans command now outputs a code.po file with code only
strings (without database content strings) and without obsolete
strings.
stats/management/commands/update-trans.py | 89 +++++++++++++++++--------------
1 file changed, 50 insertions(+), 39 deletions(-)
---
diff --git a/stats/management/commands/update-trans.py b/stats/management/commands/update-trans.py
index 7ec9f941..fc5e256f 100644
--- a/stats/management/commands/update-trans.py
+++ b/stats/management/commands/update-trans.py
@@ -1,8 +1,10 @@
-import itertools
import os
import re
import shutil
+import itertools
+import subprocess
+
from django.conf import settings
from django.core.management import call_command
from django.core.management.base import BaseCommand
@@ -15,51 +17,60 @@ from teams.models import Team
class Command(BaseCommand):
help = "Update translations of djamnedlies ('en' is a special case, and generate damned-lies.pot)"
+ database_content_filename = "database-content.py"
def add_arguments(self, parser):
- parser.add_argument('lang_code', help="language code ('it', 'pt_BR', etc.)")
- # parser.add_argument('--pot', action='store_true', dest='pot', default=False,
- # help="create a pot file"),
+ parser.add_argument("lang_code", help="language code ('it', 'pt_BR', etc.)")
def handle(self, **options):
- lang_code = options['lang_code']
+ self.lang_code = options["lang_code"]
+ self.po_dir = settings.BASE_DIR / "po"
+ if self.lang_code != "en":
+ self.po_file = self.po_dir / f"{self.lang_code}.po"
+ self.locale_dir = settings.BASE_DIR / "locale" / self.lang_code / "LC_MESSAGES"
- # Copy po/ll.po in locale/ll/LC_MESSAGES/django.po
- podir = settings.BASE_DIR / 'po'
- localedir = settings.BASE_DIR / 'locale' / lang_code / 'LC_MESSAGES'
- if lang_code != 'en':
- pofile = podir / f'{lang_code}.po'
- if pofile.exists():
- if not localedir.is_dir():
- localedir.mkdir(parents=True, exist_ok=True)
- shutil.copy(pofile, localedir / 'django.po')
- else:
- pofile = podir / 'damned-lies.pot'
+ self._update_django_po_with_code_strings()
+ # backup code only strings in the code.po file. Remove non code related msgid (mainly coming from
database)
+ code_po_file = str(self.locale_dir / "code.po")
+ shutil.copy(str(self.locale_dir / "django.po"), code_po_file)
+ subprocess.run(["/usr/bin/msgattrib", "--no-obsolete", "-o", str(code_po_file), str(code_po_file)],
check=True)
- # Extract DB translatable strings into database-content.py
- dbfile = settings.BASE_DIR / 'database-content.py'
- with dbfile.open('w') as fh:
- for value in itertools.chain(
- Team.objects.values_list('description', flat=True),
- Language.objects.exclude(name__exact=F('locale')).values_list('name', flat=True),
- Domain.objects.distinct().values_list('description', flat=True),
- Module.objects.exclude(name__exact=F('description')).values_list('description', flat=True),
- Module.objects.exclude(comment='').values_list('comment', flat=True),
- Release.objects.values_list('description', flat=True),
- CategoryName.objects.values_list('name', flat=True)
- ):
- if value:
- value = re.sub(r'\r\n|\r|\n', '\n', value)
- fh.write("_(u\"\"\"%s\"\"\")\n" % value)
+ self._update_django_po_with_database_strings()
- # Run makemessages -l ll
- os.chdir(settings.BASE_DIR)
- call_command('makemessages', locale=[lang_code])
+ # Copy locale/ll/LC_MESSAGES/django.po to po/ll.po
+ shutil.copy(str(self.locale_dir / "django.po"), str(self.po_file))
+ return "po file for language '%s' updated." % self.lang_code
- # Delete database-content.py
- dbfile.unlink()
+ def _update_django_po_with_code_strings(self):
+ if self.po_file.exists():
+ if not self.locale_dir.is_dir():
+ self.locale_dir.mkdir(parents=True, exist_ok=True)
+ shutil.copy(self.po_file, self.locale_dir / "django.po")
+ else:
+ self.po_file = self.po_dir / "damned-lies.pot"
+ self._call_django_makemessages_command()
- # Copy locale/ll/LC_MESSAGES/django.po to po/ll.po
- shutil.copy(localedir / 'django.po', pofile)
+ def _call_django_makemessages_command(self):
+ """
+ Run makemessages -l ll
+ """
+ os.chdir(settings.BASE_DIR)
+ call_command("makemessages", locale=[self.lang_code])
- return "po file for language '%s' updated." % lang_code
+ def _update_django_po_with_database_strings(self):
+ self.dbfile = settings.BASE_DIR / self.database_content_filename
+ with self.dbfile.open("w") as fh:
+ for value in itertools.chain(
+ Team.objects.values_list("description", flat=True),
+ Language.objects.exclude(name__exact=F("locale")).values_list("name", flat=True),
+ Domain.objects.distinct().values_list("description", flat=True),
+ Module.objects.exclude(name__exact=F("description")).values_list("description", flat=True),
+ Module.objects.exclude(comment="").values_list("comment", flat=True),
+ Release.objects.values_list("description", flat=True),
+ CategoryName.objects.values_list("name", flat=True),
+ ):
+ if value:
+ value = re.sub(r"\r\n|\r|\n", "\n", value)
+ fh.write('_(u"""%s""")\n' % value)
+ self._call_django_makemessages_command()
+ self.dbfile.unlink()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]