Skip to content

Commit 086f070

Browse files
author
Jakob Probst
committed
CommentHandling: add support for reading #|
- write obsolete entries entirely - add support for reading "previous" (#| ) comments
1 parent 78c4282 commit 086f070

File tree

5 files changed

+221
-171
lines changed

5 files changed

+221
-171
lines changed

babel/messages/catalog.py

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,22 @@
1111

1212
import re
1313
import time
14-
1514
from cgi import parse_header
1615
from collections import OrderedDict
16+
from copy import copy
1717
from datetime import datetime, time as time_
1818
from difflib import get_close_matches
1919
from email import message_from_string
20-
from copy import copy
2120

2221
from babel import __version__ as VERSION
22+
from babel._compat import string_types, number_types, PY2, cmp, text_type, force_text
2323
from babel.core import Locale, UnknownLocaleError
2424
from babel.dates import format_datetime
2525
from babel.messages.plurals import get_plural
2626
from babel.util import distinct, LOCALTZ, FixedOffsetTimezone
27-
from babel._compat import string_types, number_types, PY2, cmp, text_type, force_text
2827

2928
__all__ = ['Message', 'Catalog', 'TranslationError']
3029

31-
3230
PYTHON_FORMAT = re.compile(r'''
3331
\%
3432
(?:\(([\w]*)\))?
@@ -76,8 +74,8 @@ def _parse_datetime_header(value):
7674
class Message(object):
7775
"""Representation of a single message in a catalog."""
7876

79-
def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
80-
user_comments=(), previous_id=(), lineno=None, context=None):
77+
def __init__(self, id, string=u'', locations=(), flags=(), extracted_comments=(),
78+
translator_comments=(), previous_id=(), previous_context=None, lineno=None, context=None):
8179
"""Create the message object.
8280
8381
:param id: the message ID, or a ``(singular, plural)`` tuple for
@@ -86,10 +84,11 @@ def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
8684
``(singular, plural)`` tuple for pluralizable messages
8785
:param locations: a sequence of ``(filename, lineno)`` tuples
8886
:param flags: a set or sequence of flags
89-
:param auto_comments: a sequence of automatic comments for the message
90-
:param user_comments: a sequence of user comments for the message
87+
:param extracted_comments: a sequence of extracted comments for the message
88+
:param translator_comments: a sequence of translator comments for the message
9189
:param previous_id: the previous message ID, or a ``(singular, plural)``
9290
tuple for pluralizable messages
91+
:param previous_context: the previous message context
9392
:param lineno: the line number on which the msgid line was found in the
9493
PO file, if any
9594
:param context: the message context
@@ -104,12 +103,10 @@ def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
104103
self.flags.add('python-format')
105104
else:
106105
self.flags.discard('python-format')
107-
self.auto_comments = list(distinct(auto_comments))
108-
self.user_comments = list(distinct(user_comments))
109-
if isinstance(previous_id, string_types):
110-
self.previous_id = [previous_id]
111-
else:
112-
self.previous_id = list(previous_id)
106+
self.extracted_comments = list(distinct(extracted_comments))
107+
self.translator_comments = list(distinct(translator_comments))
108+
self.previous_id = previous_id
109+
self.previous_context = previous_context
113110
self.lineno = lineno
114111
self.context = context
115112

@@ -119,10 +116,12 @@ def __repr__(self):
119116

120117
def __cmp__(self, other):
121118
"""Compare Messages, taking into account plural ids"""
119+
122120
def values_to_compare(obj):
123121
if isinstance(obj, Message) and obj.pluralizable:
124122
return obj.id[0], obj.context or ''
125123
return obj.id, obj.context or ''
124+
126125
return cmp(values_to_compare(self), values_to_compare(other))
127126

128127
def __gt__(self, other):
@@ -145,9 +144,10 @@ def __ne__(self, other):
145144

146145
def clone(self):
147146
return Message(*map(copy, (self.id, self.string, self.locations,
148-
self.flags, self.auto_comments,
149-
self.user_comments, self.previous_id,
150-
self.lineno, self.context)))
147+
self.flags, self.extracted_comments,
148+
self.translator_comments, self.previous_id,
149+
self.previous_context, self.lineno,
150+
self.context)))
151151

152152
def check(self, catalog=None):
153153
"""Run various validation checks on the message. Some validations
@@ -223,7 +223,6 @@ class TranslationError(Exception):
223223
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
224224
#"""
225225

226-
227226
if PY2:
228227
def _parse_header(header_string):
229228
# message_from_string only works for str, not for unicode
@@ -336,9 +335,9 @@ def _get_header_comment(self):
336335
if hasattr(self.revision_date, 'strftime'):
337336
year = self.revision_date.strftime('%Y')
338337
comment = comment.replace('PROJECT', self.project) \
339-
.replace('VERSION', self.version) \
340-
.replace('YEAR', year) \
341-
.replace('ORGANIZATION', self.copyright_holder)
338+
.replace('VERSION', self.version) \
339+
.replace('YEAR', year) \
340+
.replace('ORGANIZATION', self.copyright_holder)
342341
locale_name = (self.locale.english_name if self.locale else self.locale_identifier)
343342
if locale_name:
344343
comment = comment.replace('Translations template', '%s translations' % locale_name)
@@ -617,26 +616,26 @@ def __setitem__(self, id, message):
617616
current.string = message.string
618617
current.locations = list(distinct(current.locations +
619618
message.locations))
620-
current.auto_comments = list(distinct(current.auto_comments +
621-
message.auto_comments))
622-
current.user_comments = list(distinct(current.user_comments +
623-
message.user_comments))
619+
current.extracted_comments = list(distinct(current.extracted_comments +
620+
message.extracted_comments))
621+
current.translator_comments = list(distinct(current.translator_comments +
622+
message.translator_comments))
624623
current.flags |= message.flags
625624
message = current
626625
elif id == '':
627626
# special treatment for the header message
628627
self.mime_headers = _parse_header(message.string).items()
629628
self.header_comment = '\n'.join([('# %s' % c).rstrip() for c
630-
in message.user_comments])
629+
in message.translator_comments])
631630
self.fuzzy = message.fuzzy
632631
else:
633632
if isinstance(id, (list, tuple)):
634633
assert isinstance(message.string, (list, tuple)), \
635634
'Expected sequence but got %s' % type(message.string)
636635
self._messages[key] = message
637636

638-
def add(self, id, string=None, locations=(), flags=(), auto_comments=(),
639-
user_comments=(), previous_id=(), lineno=None, context=None):
637+
def add(self, id, string=None, locations=(), flags=(), extracted_comments=(),
638+
translator_comments=(), previous_id=(), previous_context=None, lineno=None, context=None):
640639
"""Add or update the message with the specified ID.
641640
642641
>>> catalog = Catalog()
@@ -654,17 +653,17 @@ def add(self, id, string=None, locations=(), flags=(), auto_comments=(),
654653
``(singular, plural)`` tuple for pluralizable messages
655654
:param locations: a sequence of ``(filename, lineno)`` tuples
656655
:param flags: a set or sequence of flags
657-
:param auto_comments: a sequence of automatic comments
658-
:param user_comments: a sequence of user comments
656+
:param extracted_comments: a sequence of extracted comments
657+
:param translator_comments: a sequence of translater comments
659658
:param previous_id: the previous message ID, or a ``(singular, plural)``
660659
tuple for pluralizable messages
660+
:param previous_context: the previous message context
661661
:param lineno: the line number on which the msgid line was found in the
662662
PO file, if any
663663
:param context: the message context
664664
"""
665-
message = Message(id, string, list(locations), flags, auto_comments,
666-
user_comments, previous_id, lineno=lineno,
667-
context=context)
665+
message = Message(id, string, list(locations), flags, extracted_comments,
666+
translator_comments, previous_id, previous_context, lineno, context)
668667
self[id] = message
669668
return message
670669

@@ -700,7 +699,7 @@ def delete(self, id, context=None):
700699
if key in self._messages:
701700
del self._messages[key]
702701

703-
def update(self, template, no_fuzzy_matching=False, update_header_comment=False, keep_user_comments=True):
702+
def update(self, template, no_fuzzy_matching=False, update_header_comment=False, keep_translator_comments=True):
704703
"""Update the catalog based on the given template catalog.
705704
706705
>>> from babel.messages import Catalog
@@ -773,16 +772,13 @@ def _merge(message, oldkey, newkey):
773772
fuzzy = True
774773
fuzzy_matches.add(oldkey)
775774
oldmsg = messages.get(oldkey)
776-
if isinstance(oldmsg.id, string_types):
777-
message.previous_id = [oldmsg.id]
778-
else:
779-
message.previous_id = list(oldmsg.id)
775+
message.previous_id = oldmsg.id
780776
else:
781777
oldmsg = remaining.pop(oldkey, None)
782778
message.string = oldmsg.string
783779

784-
if keep_user_comments:
785-
message.user_comments = list(distinct(oldmsg.user_comments))
780+
if keep_translator_comments:
781+
message.translator_comments = list(distinct(oldmsg.translator_comments))
786782

787783
if isinstance(message.id, (list, tuple)):
788784
if not isinstance(message.string, (list, tuple)):

babel/messages/frontend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def callback(filename, method, options):
485485
filepath = os.path.normpath(os.path.join(path, filename))
486486

487487
catalog.add(message, None, [(filepath, lineno)],
488-
auto_comments=comments, context=context)
488+
extracted_comments=comments, context=context)
489489

490490
self.log.info('writing PO template file to %s', self.output_file)
491491
write_po(outfile, catalog, width=self.width,

0 commit comments

Comments
 (0)