Skip to content

Commit

Permalink
Fix duplicate locations when writing without lineno
Browse files Browse the repository at this point in the history
If the same translation appears multiple times in the same file,
duplicate locations would be written to the .po file when using
write_po(..., include_lineno=False).
  • Loading branch information
ljodal authored and akx committed Apr 8, 2022
1 parent bc7a51b commit 7e90d6b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions babel/messages/pofile.py
Expand Up @@ -597,9 +597,11 @@ def _write_message(message, prefix=''):

for filename, lineno in locations:
if lineno and include_lineno:
locs.append(u'%s:%d' % (filename.replace(os.sep, '/'), lineno))
location = u'%s:%d' % (filename.replace(os.sep, '/'), lineno)
else:
locs.append(u'%s' % filename.replace(os.sep, '/'))
location = u'%s' % filename.replace(os.sep, '/')
if location not in locs:
locs.append(location)
_write_comment(' '.join(locs), prefix=':')
if message.flags:
_write('#%s\n' % ', '.join([''] + sorted(message.flags)))
Expand Down
1 change: 1 addition & 0 deletions tests/messages/test_pofile.py
Expand Up @@ -827,6 +827,7 @@ def test_include_lineno(self):
def test_no_include_lineno(self):
catalog = Catalog()
catalog.add(u'foo', locations=[('main.py', 1)])
catalog.add(u'foo', locations=[('main.py', 2)])
catalog.add(u'foo', locations=[('utils.py', 3)])
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=True, include_lineno=False)
Expand Down

0 comments on commit 7e90d6b

Please sign in to comment.