Skip to content

Commit

Permalink
fix: Partial files are not added
Browse files Browse the repository at this point in the history
By mistake, we use (django_partial) and (djangojs_partial) naming
instead of (django-partial.po) and (djangojs-partial.po)

This is considered a patch to #129
  • Loading branch information
shadinaif committed Sep 2, 2023
1 parent 53b8f06 commit 55c7354
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
26 changes: 17 additions & 9 deletions i18n/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
EDX_MARKER = "edX translation file"
LOG = logging.getLogger(__name__)
DEVNULL = open(os.devnull, 'wb') # pylint: disable=consider-using-with
DJANGO_PO = 'django.po'
DJANGO_PARTIAL_PO = 'django-partial.po'
DJANGO_SAVED_PO = 'django-saved.po'
DJANGOJS_PO = 'djangojs.po'
DJANGOJS_PARTIAL_PO = 'djangojs-partial.po'
DJANGOJS_SAVED_PO = 'djangojs-saved.po'
MAKO_PO = 'mako.po'
UNDERSCORE_PO = 'underscore.po'


def file_exists(path_name):
Expand Down Expand Up @@ -79,8 +87,8 @@ def run(self, args):

# The extraction process clobbers django.po and djangojs.po.
# Save them so that it won't do that.
self.rename_source_file('django.po', 'django-saved.po')
self.rename_source_file('djangojs.po', 'djangojs-saved.po')
self.rename_source_file(DJANGO_PO, DJANGO_SAVED_PO)
self.rename_source_file(DJANGOJS_PO, DJANGOJS_SAVED_PO)

# Extract strings from mako templates.
verbosity_map = {
Expand Down Expand Up @@ -112,11 +120,11 @@ def run(self, args):

# makemessages creates 'django.po'. This filename is hardcoded.
# Rename it to django-partial.po to enable merging into django.po later.
self.rename_source_file('django.po', 'django-partial.po')
self.rename_source_file(DJANGO_PO, DJANGO_PARTIAL_PO)

# makemessages creates 'djangojs.po'. This filename is hardcoded.
# Rename it to djangojs-partial.po to enable merging into djangojs.po later.
self.rename_source_file('djangojs.po', 'djangojs-partial.po')
self.rename_source_file(DJANGOJS_PO, DJANGOJS_PARTIAL_PO)

files_to_clean = set()

Expand All @@ -143,15 +151,15 @@ def run(self, args):
files_to_clean.update(segmented_files)

# Add partial files to the list of files to clean.
files_to_clean.update(('django_partial', 'djangojs_partial'))
files_to_clean.update((DJANGO_PARTIAL_PO, DJANGOJS_PARTIAL_PO))

# Finish each file.
for filename in files_to_clean:
clean_pofile(self.source_msgs_dir.joinpath(filename))

# Restore the saved .po files.
self.rename_source_file('django-saved.po', 'django.po')
self.rename_source_file('djangojs-saved.po', 'djangojs.po')
self.rename_source_file(DJANGO_SAVED_PO, DJANGO_PO)
self.rename_source_file(DJANGOJS_SAVED_PO, DJANGOJS_PO)

def babel_extract(self, stderr, verbosity):
"""
Expand All @@ -174,7 +182,7 @@ def babel_extract(self, stderr, verbosity):
babel_mako_cmd = babel_cmd_template.format(
verbosity=verbosity,
config=babel_mako_cfg,
output=self.base(configuration.source_messages_dir, 'mako.po'),
output=self.base(configuration.source_messages_dir, MAKO_PO),
)

execute(babel_mako_cmd, working_directory=configuration.root_dir, stderr=stderr)
Expand All @@ -184,7 +192,7 @@ def babel_extract(self, stderr, verbosity):
babel_underscore_cmd = babel_cmd_template.format(
verbosity=verbosity,
config=babel_underscore_cfg,
output=self.base(configuration.source_messages_dir, 'underscore.po'),
output=self.base(configuration.source_messages_dir, UNDERSCORE_PO),
)

execute(babel_underscore_cmd, working_directory=configuration.root_dir, stderr=stderr)
Expand Down
3 changes: 2 additions & 1 deletion i18n/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@

from i18n import Runner
from i18n.execute import execute
from i18n.extract import DJANGO_PARTIAL_PO, DJANGO_PO

LOG = logging.getLogger(__name__)
DEVNULL = open(os.devnull, "wb") # pylint: disable=consider-using-with
DUPLICATE_ENTRY_PATTERN = re.compile('#-#-#-#-#.*#-#-#-#-#')


def merge(configuration, locale, target='django.po', sources=('django-partial.po',), fail_if_missing=True):
def merge(configuration, locale, target=DJANGO_PO, sources=(DJANGO_PARTIAL_PO,), fail_if_missing=True):
"""
For the given locale, merge the `sources` files to become the `target`
file. Note that the target file might also be one of the sources.
Expand Down
6 changes: 3 additions & 3 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ def django_po(self):
"""
Returns the name of the generated django file
"""
return 'django-partial.po'
return extract.DJANGO_PARTIAL_PO

@property
def djangojs_po(self):
"""
Returns the name of the generated djangojs file
"""
return 'djangojs-partial.po'
return extract.DJANGOJS_PARTIAL_PO

def get_files(self):
"""
This is a generator.
Returns the fully expanded filenames for all extracted files
Fails assertion if one of the files doesn't exist.
"""
generated_files = ('mako.po', self.django_po, self.djangojs_po,)
generated_files = (extract.MAKO_PO, self.django_po, self.djangojs_po,)

for filename in generated_files:
path = Path.joinpath(self.configuration.source_messages_dir, filename)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pytz import UTC

from i18n import config, generate
from i18n.extract import DJANGO_PARTIAL_PO, DJANGO_PO

from . import I18nToolTestCase, MOCK_APPLICATION_DIR, MOCK_DJANGO_APP_DIR

Expand Down Expand Up @@ -56,7 +57,7 @@ def test_merge_with_missing_sources(self):
generate.merge(
test_configuration,
test_configuration.source_locale,
sources=("django-partial.po", "nonexistingfile.po"),
sources=(DJANGO_PARTIAL_PO, "nonexistingfile.po"),
target=filename,
fail_if_missing=False,
)
Expand All @@ -73,7 +74,7 @@ def test_merge_with_missing_sources_strict(self):
generate.merge(
test_configuration,
test_configuration.source_locale,
sources=("django-partial.po", "nonexistingfile.po"),
sources=(DJANGO_PARTIAL_PO, "nonexistingfile.po"),
target=filename,
fail_if_missing=True,
)
Expand Down Expand Up @@ -127,7 +128,7 @@ def assert_merge_headers(self, file_path, num_headers):

@patch('i18n.generate.LOG')
def test_resolve_merge_conflicts(self, mock_log):
django_po_path = Path.joinpath(self.configuration.get_messages_dir('mock'), 'django.po')
django_po_path = Path.joinpath(self.configuration.get_messages_dir('mock'), DJANGO_PO)
# File ought to have been generated in test_main
# if not Path.exists(django_po_path):
generate.main(verbose=0, strict=False, root_dir=MOCK_APPLICATION_DIR)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from path import Path
import polib

from i18n.extract import DJANGO_PO
from i18n.segment import segment_pofile

from . import I18nToolTestCase
Expand All @@ -32,7 +33,7 @@ def assert_pofile_same(self, pofile1, pofile2):
self.assertEqual(po1, po2)

def test_sample_data(self):
work_file = WORK / "django.po"
work_file = WORK / DJANGO_PO
shutil.copyfile(TEST_DATA / "django_before.po", work_file)
original_pofile = polib.pofile(work_file)

Expand All @@ -46,7 +47,7 @@ def test_sample_data(self):
}
)

self.assertEqual(written, {WORK / "django.po", WORK / "studio.po"})
self.assertEqual(written, {WORK / DJANGO_PO, WORK / "studio.po"})

pofiles = [polib.pofile(f) for f in written]
after_entries = sum(len(pofile) for pofile in pofiles)
Expand All @@ -56,5 +57,5 @@ def test_sample_data(self):
after_ids = {m.msgid for pofile in pofiles for m in pofile}
self.assertEqual(original_ids, after_ids)

self.assert_pofile_same(WORK / "django.po", TEST_DATA / "django_after.po")
self.assert_pofile_same(WORK / DJANGO_PO, TEST_DATA / "django_after.po")
self.assert_pofile_same(WORK / "studio.po", TEST_DATA / "studio.po")

0 comments on commit 55c7354

Please sign in to comment.