11
11
12
12
import re
13
13
import time
14
-
15
14
from cgi import parse_header
16
15
from collections import OrderedDict
16
+ from copy import copy
17
17
from datetime import datetime , time as time_
18
18
from difflib import get_close_matches
19
19
from email import message_from_string
20
- from copy import copy
21
20
22
21
from babel import __version__ as VERSION
22
+ from babel ._compat import string_types , number_types , PY2 , cmp , text_type , force_text
23
23
from babel .core import Locale , UnknownLocaleError
24
24
from babel .dates import format_datetime
25
25
from babel .messages .plurals import get_plural
26
26
from babel .util import distinct , LOCALTZ , FixedOffsetTimezone
27
- from babel ._compat import string_types , number_types , PY2 , cmp , text_type , force_text
28
27
29
28
__all__ = ['Message' , 'Catalog' , 'TranslationError' ]
30
29
31
-
32
30
PYTHON_FORMAT = re .compile (r'''
33
31
\%
34
32
(?:\(([\w]*)\))?
@@ -76,8 +74,8 @@ def _parse_datetime_header(value):
76
74
class Message (object ):
77
75
"""Representation of a single message in a catalog."""
78
76
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 ):
81
79
"""Create the message object.
82
80
83
81
: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=(),
86
84
``(singular, plural)`` tuple for pluralizable messages
87
85
:param locations: a sequence of ``(filename, lineno)`` tuples
88
86
: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
91
89
:param previous_id: the previous message ID, or a ``(singular, plural)``
92
90
tuple for pluralizable messages
91
+ :param previous_context: the previous message context
93
92
:param lineno: the line number on which the msgid line was found in the
94
93
PO file, if any
95
94
:param context: the message context
@@ -104,12 +103,10 @@ def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
104
103
self .flags .add ('python-format' )
105
104
else :
106
105
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
113
110
self .lineno = lineno
114
111
self .context = context
115
112
@@ -119,10 +116,12 @@ def __repr__(self):
119
116
120
117
def __cmp__ (self , other ):
121
118
"""Compare Messages, taking into account plural ids"""
119
+
122
120
def values_to_compare (obj ):
123
121
if isinstance (obj , Message ) and obj .pluralizable :
124
122
return obj .id [0 ], obj .context or ''
125
123
return obj .id , obj .context or ''
124
+
126
125
return cmp (values_to_compare (self ), values_to_compare (other ))
127
126
128
127
def __gt__ (self , other ):
@@ -145,9 +144,10 @@ def __ne__(self, other):
145
144
146
145
def clone (self ):
147
146
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 )))
151
151
152
152
def check (self , catalog = None ):
153
153
"""Run various validation checks on the message. Some validations
@@ -223,7 +223,6 @@ class TranslationError(Exception):
223
223
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
224
224
#"""
225
225
226
-
227
226
if PY2 :
228
227
def _parse_header (header_string ):
229
228
# message_from_string only works for str, not for unicode
@@ -336,9 +335,9 @@ def _get_header_comment(self):
336
335
if hasattr (self .revision_date , 'strftime' ):
337
336
year = self .revision_date .strftime ('%Y' )
338
337
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 )
342
341
locale_name = (self .locale .english_name if self .locale else self .locale_identifier )
343
342
if locale_name :
344
343
comment = comment .replace ('Translations template' , '%s translations' % locale_name )
@@ -617,26 +616,26 @@ def __setitem__(self, id, message):
617
616
current .string = message .string
618
617
current .locations = list (distinct (current .locations +
619
618
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 ))
624
623
current .flags |= message .flags
625
624
message = current
626
625
elif id == '' :
627
626
# special treatment for the header message
628
627
self .mime_headers = _parse_header (message .string ).items ()
629
628
self .header_comment = '\n ' .join ([('# %s' % c ).rstrip () for c
630
- in message .user_comments ])
629
+ in message .translator_comments ])
631
630
self .fuzzy = message .fuzzy
632
631
else :
633
632
if isinstance (id , (list , tuple )):
634
633
assert isinstance (message .string , (list , tuple )), \
635
634
'Expected sequence but got %s' % type (message .string )
636
635
self ._messages [key ] = message
637
636
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 ):
640
639
"""Add or update the message with the specified ID.
641
640
642
641
>>> catalog = Catalog()
@@ -654,17 +653,17 @@ def add(self, id, string=None, locations=(), flags=(), auto_comments=(),
654
653
``(singular, plural)`` tuple for pluralizable messages
655
654
:param locations: a sequence of ``(filename, lineno)`` tuples
656
655
: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
659
658
:param previous_id: the previous message ID, or a ``(singular, plural)``
660
659
tuple for pluralizable messages
660
+ :param previous_context: the previous message context
661
661
:param lineno: the line number on which the msgid line was found in the
662
662
PO file, if any
663
663
:param context: the message context
664
664
"""
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 )
668
667
self [id ] = message
669
668
return message
670
669
@@ -700,7 +699,7 @@ def delete(self, id, context=None):
700
699
if key in self ._messages :
701
700
del self ._messages [key ]
702
701
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 ):
704
703
"""Update the catalog based on the given template catalog.
705
704
706
705
>>> from babel.messages import Catalog
@@ -773,16 +772,13 @@ def _merge(message, oldkey, newkey):
773
772
fuzzy = True
774
773
fuzzy_matches .add (oldkey )
775
774
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
780
776
else :
781
777
oldmsg = remaining .pop (oldkey , None )
782
778
message .string = oldmsg .string
783
779
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 ))
786
782
787
783
if isinstance (message .id , (list , tuple )):
788
784
if not isinstance (message .string , (list , tuple )):
0 commit comments