changeset 600:e61e27d9347d trunk

"init" command support "--width" option (#284)
author fschwarz
date Mon, 20 Aug 2012 20:07:27 +0000
parents 33c8c68b96c7
children 3eeaadb290be
files ChangeLog babel/messages/frontend.py babel/messages/tests/frontend.py
diffstat 3 files changed, 68 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -54,6 +54,7 @@
  * format_time() and format_datetime() now accept also floats (#242)
  * add babel.support.NullTranslations class similar to gettext.NullTranslations
    but with all of Babel's new *gettext methods (#277)
+ * "init" command support "--width" option (#284)
 
 
 Version 0.9.6
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -402,6 +402,8 @@
          "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"),
         ('locale=', 'l',
          'locale for the new localized catalog'),
+        ('width=', 'w',
+         'set output line width (default 76)'),
         ('no-wrap', None,
          'do not break long message lines, longer than the output line width, '
          'into several lines'),
@@ -437,8 +439,10 @@
 
         if not os.path.exists(os.path.dirname(self.output_file)):
             os.makedirs(os.path.dirname(self.output_file))
-        if not self.no_wrap:
+        if not self.no_wrap and not self.width:
             self.width = 76
+        elif self.width is not None:
+            self.width = int(self.width)
 
     def run(self):
         log.info('creating catalog %r based on %r', self.output_file,
@@ -959,6 +963,8 @@
                                "<domain>.po')")
         parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE',
                           help='locale for the new localized catalog')
+        parser.add_option('-w', '--width', dest='width', type='int',
+                          help="set output line width (default 76)")
         parser.add_option('--no-wrap', dest='no_wrap', action='store_true',
                           help='do not break long message lines, longer than '
                                'the output line width, into several lines')
@@ -985,9 +991,10 @@
                                                options.domain + '.po')
         if not os.path.exists(os.path.dirname(options.output_file)):
             os.makedirs(os.path.dirname(options.output_file))
-        width = 76
-        if options.no_wrap:
-            width = None
+        if options.width and options.no_wrap:
+            parser.error("'--no-wrap' and '--width' are mutually exclusive.")
+        elif not options.width and not options.no_wrap:
+            options.width = 76
 
         infile = open(options.input_file, 'r')
         try:
@@ -1005,7 +1012,7 @@
 
         outfile = open(options.output_file, 'w')
         try:
-            write_po(outfile, catalog, width=width)
+            write_po(outfile, catalog, width=options.width)
         finally:
             outfile.close()
 
--- a/babel/messages/tests/frontend.py
+++ b/babel/messages/tests/frontend.py
@@ -565,6 +565,61 @@
                                tzinfo=LOCALTZ, locale='en_US'),
        'long_message': long_message},
        open(po_file, 'U').read())
+   
+    def test_supports_width(self):
+        self.cmd.input_file = 'project/i18n/long_messages.pot'
+        self.cmd.locale = 'en_US'
+        self.cmd.output_dir = 'project/i18n'
+        
+        long_message = '"'+ 'xxxxx '*15 + '"'
+        
+        pot_contents = open('project/i18n/messages.pot', 'U').read()
+        pot_with_very_long_line = pot_contents.replace('"bar"', long_message)
+        open(self.cmd.input_file, 'wb').write(pot_with_very_long_line)
+        self.cmd.width = 120
+        self.cmd.finalize_options()
+        self.cmd.run()
+
+        po_file = self._po_file('en_US')
+        assert os.path.isfile(po_file)
+        self.assertEqual(
+r"""# English (United States) translations for TestProject.
+# Copyright (C) 2007 FooBar, Inc.
+# This file is distributed under the same license as the TestProject
+# project.
+# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: TestProject 0.1\n"
+"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
+"POT-Creation-Date: 2007-04-01 15:30+0200\n"
+"PO-Revision-Date: %(date)s\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: en_US <LL@li.org>\n"
+"Plural-Forms: nplurals=2; plural=(n != 1)\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=utf-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Generated-By: Babel %(version)s\n"
+
+#. This will be a translator coment,
+#. that will include several lines
+#: project/file1.py:8
+msgid %(long_message)s
+msgstr ""
+
+#: project/file2.py:9
+msgid "foobar"
+msgid_plural "foobars"
+msgstr[0] ""
+msgstr[1] ""
+
+""" % {'version': VERSION,
+       'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ',
+                               tzinfo=LOCALTZ, locale='en_US'),
+       'long_message': long_message},
+       open(po_file, 'U').read())
 
 
 class CommandLineInterfaceTestCase(unittest.TestCase):
Copyright (C) 2012-2017 Edgewall Software