Code

Ticket #2204: options.py.diff

File options.py.diff, 2.1 KB (added by Poromenos, 8 years ago)

Pluralization patch.

Line 
1Index: options.py
2===================================================================
3--- options.py  (revision 3181)
4+++ options.py  (working copy)
5@@ -41,6 +41,21 @@
6         self.object_name = cls.__name__
7         self.module_name = self.object_name.lower()
8         self.verbose_name = get_verbose_name(self.object_name)
9+        vowels = "aeiou"
10+        consonants = "bcdfghjklmnpqrstvwxyz"
11+        # If the word ends in a 'y' and is preceded by a vowel, add 'ies'.
12+        if self.verbose_name.endswith('y') and self.verbose_name[-2] in consonants:
13+            verbose_name_plural = self.verbose_name[:-1] + 'ies'
14+        elif self.verbose_name.endswith('is'):
15+            verbose_name_plural = self.verbose_name[:-2] + 'es'
16+        elif self.verbose_name.endswith('us'):
17+            verbose_name_plural = self.verbose_name[:-2] + 'i'
18+        elif self.verbose_name.endswith('o') and self.verbose_name[-2] in consonants:
19+            verbose_name_plural = self.verbose_name + 'es'
20+        elif self.verbose_name[-2:] in ('ch', 'sh') or self.verbose_name[-1] in ('s', 'x', 'z'):
21+            verbose_name_plural = self.verbose_name + 'es'
22+        else:
23+            verbose_name_plural = self.verbose_name + 's'
24         # Next, apply any overridden values from 'class Meta'.
25         if self.meta:
26             meta_attrs = self.meta.__dict__
27@@ -50,12 +65,12 @@
28                 setattr(self, attr_name, meta_attrs.pop(attr_name, getattr(self, attr_name)))
29             # verbose_name_plural is a special case because it uses a 's'
30             # by default.
31-            setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', self.verbose_name + 's'))
32+            setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', verbose_name_plural))
33             # Any leftover attributes must be invalid.
34             if meta_attrs != {}:
35                 raise TypeError, "'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys())
36         else:
37-            self.verbose_name_plural = self.verbose_name + 's'
38+            self.verbose_name_plural = verbose_name_plural
39         del self.meta
40 
41     def _prepare(self, model):