Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6505 - Copy plural forms from Django translation files for new…

…ly created translation files. Thanks to Ramiro Morales for the initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12445 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 89df0432f21909b25b690dc5ebe479a025a62bd0 1 parent eb26c96
Jannis Leidel authored February 16, 2010
36  django/core/management/commands/makemessages.py
@@ -11,6 +11,7 @@
11 11
 from django.utils.text import get_text_list
12 12
 
13 13
 pythonize_re = re.compile(r'(?:^|\n)\s*//')
  14
+plural_forms_re = re.compile(r'^(?P<value>"Plural-Forms.+?\\n")\s*$', re.MULTILINE | re.DOTALL)
14 15
 
15 16
 def handle_extensions(extensions=('html',)):
16 17
     """
@@ -82,6 +83,37 @@ def find_files(root, ignore_patterns, verbosity, symlinks=False):
82 83
     all_files.sort()
83 84
     return all_files
84 85
 
  86
+def copy_plural_forms(msgs, locale, domain, verbosity):
  87
+    """
  88
+    Copies plural forms header contents from a Django catalog of locale to
  89
+    the msgs string, inserting it at the right place. msgs should be the
  90
+    contents of a newly created .po file.
  91
+    """
  92
+    import django
  93
+    django_dir = os.path.normpath(os.path.join(os.path.dirname(django.__file__)))
  94
+    if domain == 'djangojs':
  95
+        domains = ('djangojs', 'django')
  96
+    else:
  97
+        domains = ('django',)
  98
+    for domain in domains:
  99
+        django_po = os.path.join(django_dir, 'conf', 'locale', locale, 'LC_MESSAGES', '%s.po' % domain)
  100
+        if os.path.exists(django_po):
  101
+            m = plural_forms_re.search(open(django_po, 'rU').read())
  102
+            if m:
  103
+                if verbosity > 1:
  104
+                    sys.stderr.write("copying plural forms: %s\n" % m.group('value'))
  105
+                lines = []
  106
+                seen = False
  107
+                for line in msgs.split('\n'):
  108
+                    if not line and not seen:
  109
+                        line = '%s\n' % m.group('value')
  110
+                        seen = True
  111
+                    lines.append(line)
  112
+                msgs = '\n'.join(lines)
  113
+                break
  114
+    return msgs
  115
+
  116
+
85 117
 def make_messages(locale=None, domain='django', verbosity='1', all=False,
86 118
         extensions=None, symlinks=False, ignore_patterns=[]):
87 119
     """
@@ -97,8 +129,10 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
97 129
 
98 130
     from django.utils.translation import templatize
99 131
 
  132
+    invoked_for_django = False
100 133
     if os.path.isdir(os.path.join('conf', 'locale')):
101 134
         localedir = os.path.abspath(os.path.join('conf', 'locale'))
  135
+        invoked_for_django = True
102 136
     elif os.path.isdir('locale'):
103 137
         localedir = os.path.abspath('locale')
104 138
     else:
@@ -208,6 +242,8 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
208 242
                 msgs, errors = _popen('msgmerge -q "%s" "%s"' % (pofile, potfile))
209 243
                 if errors:
210 244
                     raise CommandError("errors happened while running msgmerge\n%s" % errors)
  245
+            elif not invoked_for_django:
  246
+                msgs = copy_plural_forms(msgs, locale, domain, verbosity)
211 247
             open(pofile, 'wb').write(msgs)
212 248
             os.unlink(potfile)
213 249
 
16  tests/regressiontests/makemessages/tests.py
@@ -8,6 +8,8 @@
8 8
 
9 9
 class ExtractorTests(TestCase):
10 10
 
  11
+    PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
  12
+
11 13
     def setUp(self):
12 14
         self._cwd = os.getcwd()
13 15
         self.test_dir = os.path.abspath(os.path.dirname(__file__))
@@ -47,8 +49,6 @@ def test_javascript_literals(self):
47 49
 
48 50
 class IgnoredExtractorTests(ExtractorTests):
49 51
 
50  
-    PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
51  
-
52 52
     def test_ignore_option(self):
53 53
         os.chdir(self.test_dir)
54 54
         management.call_command('makemessages', locale=LOCALE, verbosity=0, ignore_patterns=['ignore_dir/*'])
@@ -60,8 +60,6 @@ def test_ignore_option(self):
60 60
 
61 61
 class SymlinkExtractorTests(ExtractorTests):
62 62
 
63  
-    PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
64  
-
65 63
     def setUp(self):
66 64
         self._cwd = os.getcwd()
67 65
         self.test_dir = os.path.abspath(os.path.dirname(__file__))
@@ -88,3 +86,13 @@ def test_symlink(self):
88 86
             po_contents = open(self.PO_FILE, 'r').read()
89 87
             self.assertMsgId('This literal should be included.', po_contents)
90 88
             self.assert_('templates_symlinked/test.html' in po_contents)
  89
+
  90
+
  91
+class CopyPluralFormsExtractorTests(ExtractorTests):
  92
+
  93
+    def test_copy_plural_forms(self):
  94
+        os.chdir(self.test_dir)
  95
+        management.call_command('makemessages', locale=LOCALE, verbosity=0)
  96
+        self.assert_(os.path.exists(self.PO_FILE))
  97
+        po_contents = open(self.PO_FILE, 'r').read()
  98
+        self.assert_('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)

0 notes on commit 89df043

Please sign in to comment.
Something went wrong with that request. Please try again.