]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
"init" command support "--width" option (#284)
authorFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Mon, 20 Aug 2012 20:07:27 +0000 (20:07 +0000)
committerFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Mon, 20 Aug 2012 20:07:27 +0000 (20:07 +0000)
ChangeLog
babel/messages/frontend.py
babel/messages/tests/frontend.py

index 516a72bd2bd70414080b0383aa71b5541b64c931..7bdcc80cee9030a4b52425c386d4544531bf0f97 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -54,6 +54,7 @@ http://svn.edgewall.org/repos/babel/tags/1.0.0/
  * 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
index 6bfabecb878230c0a216f0954c0131765182ea47..03de28cf49b68850dbda341730b2ee60f1ffd1ad 100755 (executable)
@@ -402,6 +402,8 @@ class init_catalog(Command):
          "'<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 @@ class init_catalog(Command):
 
         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 @@ class CommandLineInterface(object):
                                "<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 @@ class CommandLineInterface(object):
                                                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 @@ class CommandLineInterface(object):
 
         outfile = open(options.output_file, 'w')
         try:
-            write_po(outfile, catalog, width=width)
+            write_po(outfile, catalog, width=options.width)
         finally:
             outfile.close()
 
index 04f264b9ab2e399a0e54738e1d2fd35b33867566..48b2e6a9c8405758b1a5063645c9ab1c6a89ca8c 100644 (file)
@@ -560,6 +560,61 @@ 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())
+   
+    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'),