From: Christopher Lenz Date: Sun, 1 Jul 2007 17:59:44 +0000 (+0000) Subject: Add an option to the frontend commands for catalog updating that removes completely... X-Git-Tag: 1.0~455 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3130fbce4a5789ab21f517bc920fdf11b092eaee;p=thirdparty%2Fbabel.git 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 index 732ec014..2ca56936 100755 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -452,7 +452,10 @@ class update_catalog(Command): "'//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 @@ class update_catalog(Command): 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 @@ class update_catalog(Command): 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 @@ class CommandLineInterface(object): ".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 @@ class CommandLineInterface(object): 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 index 8b06122a..1f6e2dd4 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -298,7 +298,7 @@ def normalize(string, prefix='', width=76): 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 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, :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 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, _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 index 94a7d224..3e6c5b3a 100644 --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -194,6 +194,18 @@ msgstr "Voh" #~ "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 index a2ea456b..dcf247ae 100644 --- a/doc/cmdline.txt +++ b/doc/cmdline.txt @@ -170,6 +170,8 @@ a PO template file:: '//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 index 29b254d4..ba5ddc26 100644 --- a/doc/setup.txt +++ b/doc/setup.txt @@ -323,6 +323,9 @@ The ``update_catalog`` command accepts the following options: +-----------------------------+----------------------------------------------+ | ``--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::