# HG changeset patch # User cmlenz # Date 1183312784 0 # Node ID b5e58a22ebd2b6a8099362fac8fccd281a5fe125 # Parent 8f5805197198ec8976a5083945bacfc1adb5bba1 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments. diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -452,7 +452,10 @@ "'//LC_MESSAGES/.po')"), ('locale=', 'l', 'locale of the catalog to compile'), + ('ignore-obsolete=', None, + 'whether to omit obsolete messages from the output') ] + boolean_options = ['ignore_obsolete'] def initialize_options(self): self.domain = 'messages' @@ -460,6 +463,7 @@ self.output_dir = None self.output_file = None self.locale = None + self.ignore_obsolete = False def finalize_options(self): if not self.input_file: @@ -504,7 +508,7 @@ outfile = open(po_file, 'w') try: - write_po(outfile, catalog) + write_po(outfile, catalog, ignore_obsolete=self.ignore_obsolete) finally: outfile.close() @@ -853,8 +857,12 @@ ".po')") parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', help='locale of the translations catalog') + parser.add_option('--ignore-obsolete', dest='ignore_obsolete', + action='store_true', + help='do not include obsolete messages in the output ' + '(default %default)'), - parser.set_defaults(domain='messages') + parser.set_defaults(domain='messages', ignore_obsolete=False) options, args = parser.parse_args(argv) if not options.input_file: @@ -898,7 +906,8 @@ outfile = open(po_file, 'w') try: - write_po(outfile, catalog) + write_po(outfile, catalog, + ignore_obsolete=options.ignore_obsolete) finally: outfile.close() diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -298,7 +298,7 @@ return u'""\n' + u'\n'.join([(prefix + escape(l)) for l in lines]) def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, - sort_output=False, sort_by_file=False): + sort_output=False, sort_by_file=False, ignore_obsolete=False): r"""Write a ``gettext`` PO (portable object) template file for a given message catalog to the provided file-like object. @@ -330,6 +330,10 @@ :param no_location: do not emit a location comment for every message :param omit_header: do not include the ``msgid ""`` entry at the top of the output + :sort_output: whether to sort the messages in the output by msgid + :sort_by_file: whether to sort the messages in the output by their locations + :ignore_obsolete: whether to ignore obsolete messages and not include them + in the output; by default they are included as comments """ def _normalize(key, prefix=''): return normalize(key, prefix=prefix, width=width) \ @@ -397,8 +401,9 @@ _write_message(message) _write('\n') - for message in catalog.obsolete.values(): - for comment in message.user_comments: - _write_comment(comment) - _write_message(message, prefix='#~ ') - _write('\n') + if not ignore_obsolete: + for message in catalog.obsolete.values(): + for comment in message.user_comments: + _write_comment(comment) + _write_message(message, prefix='#~ ') + _write('\n') diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -194,6 +194,18 @@ #~ "multiple lines, and should still be handled\n" #~ "correctly.\n"''', buf.getvalue().strip()) + def test_po_with_obsolete_message_ignored(self): + catalog = Catalog() + catalog.add(u'foo', u'Voh', locations=[('main.py', 1)]) + catalog.obsolete['bar'] = Message(u'bar', u'Bahr', + locations=[('utils.py', 3)], + user_comments=['User comment']) + buf = StringIO() + pofile.write_po(buf, catalog, omit_header=True, ignore_obsolete=True) + self.assertEqual('''#: main.py:1 +msgid "foo" +msgstr "Voh"''', buf.getvalue().strip()) + def suite(): suite = unittest.TestSuite() diff --git a/doc/cmdline.txt b/doc/cmdline.txt --- a/doc/cmdline.txt +++ b/doc/cmdline.txt @@ -170,6 +170,8 @@ '//LC_MESSAGES/.po') -l LOCALE, --locale=LOCALE locale of the translations catalog + --ignore-obsolete do not include obsolete messages in the output + (default False) If ``output_dir`` is specified, but ``output-file`` is not, the default filename of the output file will be:: diff --git a/doc/setup.txt b/doc/setup.txt --- a/doc/setup.txt +++ b/doc/setup.txt @@ -323,6 +323,9 @@ +-----------------------------+----------------------------------------------+ | ``--locale`` | locale for the new localized string | +-----------------------------+----------------------------------------------+ + | ``--ignore-obsolete`` | do not include obsolete messages in the | + | | output | + +-----------------------------+----------------------------------------------+ If ``output-dir`` is specified, but ``output-file`` is not, the default filename of the output file will be::