Author: jezdez
Date: 2010-02-16 06:15:41 -0600 (Tue, 16 Feb 2010)
New Revision: 12445

Modified:
   django/trunk/django/core/management/commands/makemessages.py
   django/trunk/tests/regressiontests/makemessages/tests.py
Log:
Fixed #6505 - Copy plural forms from Django translation files for newly created 
translation files. Thanks to Ramiro Morales for the initial patch.

Modified: django/trunk/django/core/management/commands/makemessages.py
===================================================================
--- django/trunk/django/core/management/commands/makemessages.py        
2010-02-16 12:15:04 UTC (rev 12444)
+++ django/trunk/django/core/management/commands/makemessages.py        
2010-02-16 12:15:41 UTC (rev 12445)
@@ -11,6 +11,7 @@
 from django.utils.text import get_text_list
 
 pythonize_re = re.compile(r'(?:^|\n)\s*//')
+plural_forms_re = re.compile(r'^(?P<value>"Plural-Forms.+?\\n")\s*$', 
re.MULTILINE | re.DOTALL)
 
 def handle_extensions(extensions=('html',)):
     """
@@ -82,6 +83,37 @@
     all_files.sort()
     return all_files
 
+def copy_plural_forms(msgs, locale, domain, verbosity):
+    """
+    Copies plural forms header contents from a Django catalog of locale to
+    the msgs string, inserting it at the right place. msgs should be the
+    contents of a newly created .po file.
+    """
+    import django
+    django_dir = 
os.path.normpath(os.path.join(os.path.dirname(django.__file__)))
+    if domain == 'djangojs':
+        domains = ('djangojs', 'django')
+    else:
+        domains = ('django',)
+    for domain in domains:
+        django_po = os.path.join(django_dir, 'conf', 'locale', locale, 
'LC_MESSAGES', '%s.po' % domain)
+        if os.path.exists(django_po):
+            m = plural_forms_re.search(open(django_po, 'rU').read())
+            if m:
+                if verbosity > 1:
+                    sys.stderr.write("copying plural forms: %s\n" % 
m.group('value'))
+                lines = []
+                seen = False
+                for line in msgs.split('\n'):
+                    if not line and not seen:
+                        line = '%s\n' % m.group('value')
+                        seen = True
+                    lines.append(line)
+                msgs = '\n'.join(lines)
+                break
+    return msgs
+
+
 def make_messages(locale=None, domain='django', verbosity='1', all=False,
         extensions=None, symlinks=False, ignore_patterns=[]):
     """
@@ -97,8 +129,10 @@
 
     from django.utils.translation import templatize
 
+    invoked_for_django = False
     if os.path.isdir(os.path.join('conf', 'locale')):
         localedir = os.path.abspath(os.path.join('conf', 'locale'))
+        invoked_for_django = True
     elif os.path.isdir('locale'):
         localedir = os.path.abspath('locale')
     else:
@@ -208,6 +242,8 @@
                 msgs, errors = _popen('msgmerge -q "%s" "%s"' % (pofile, 
potfile))
                 if errors:
                     raise CommandError("errors happened while running 
msgmerge\n%s" % errors)
+            elif not invoked_for_django:
+                msgs = copy_plural_forms(msgs, locale, domain, verbosity)
             open(pofile, 'wb').write(msgs)
             os.unlink(potfile)
 

Modified: django/trunk/tests/regressiontests/makemessages/tests.py
===================================================================
--- django/trunk/tests/regressiontests/makemessages/tests.py    2010-02-16 
12:15:04 UTC (rev 12444)
+++ django/trunk/tests/regressiontests/makemessages/tests.py    2010-02-16 
12:15:41 UTC (rev 12445)
@@ -8,6 +8,8 @@
 
 class ExtractorTests(TestCase):
 
+    PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
+
     def setUp(self):
         self._cwd = os.getcwd()
         self.test_dir = os.path.abspath(os.path.dirname(__file__))
@@ -47,8 +49,6 @@
 
 class IgnoredExtractorTests(ExtractorTests):
 
-    PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
-
     def test_ignore_option(self):
         os.chdir(self.test_dir)
         management.call_command('makemessages', locale=LOCALE, verbosity=0, 
ignore_patterns=['ignore_dir/*'])
@@ -60,8 +60,6 @@
 
 class SymlinkExtractorTests(ExtractorTests):
 
-    PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
-
     def setUp(self):
         self._cwd = os.getcwd()
         self.test_dir = os.path.abspath(os.path.dirname(__file__))
@@ -88,3 +86,13 @@
             po_contents = open(self.PO_FILE, 'r').read()
             self.assertMsgId('This literal should be included.', po_contents)
             self.assert_('templates_symlinked/test.html' in po_contents)
+
+
+class CopyPluralFormsExtractorTests(ExtractorTests):
+
+    def test_copy_plural_forms(self):
+        os.chdir(self.test_dir)
+        management.call_command('makemessages', locale=LOCALE, verbosity=0)
+        self.assert_(os.path.exists(self.PO_FILE))
+        po_contents = open(self.PO_FILE, 'r').read()
+        self.assert_('Plural-Forms: nplurals=2; plural=(n != 1)' in 
po_contents)

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to