cc7b79e3e44018a91c3a47c4e1ba243a52e65208
[sdc/sdc-distribution-client.git] /
1 # -*- coding: utf-8 -*-
2 """
3     babel.messages.plurals
4     ~~~~~~~~~~~~~~~~~~~~~~
5
6     Plural form definitions.
7
8     :copyright: (c) 2013 by the Babel Team.
9     :license: BSD, see LICENSE for more details.
10 """
11
12 from babel.core import default_locale, Locale
13 from operator import itemgetter
14
15
16 # XXX: remove this file, duplication with babel.plural
17
18
19 LC_CTYPE = default_locale('LC_CTYPE')
20
21
22 PLURALS = {
23     # Afar
24     # 'aa': (),
25     # Abkhazian
26     # 'ab': (),
27     # Avestan
28     # 'ae': (),
29     # Afrikaans - From Pootle's PO's
30     'af': (2, '(n != 1)'),
31     # Akan
32     # 'ak': (),
33     # Amharic
34     # 'am': (),
35     # Aragonese
36     # 'an': (),
37     # Arabic - From Pootle's PO's
38     'ar': (6, '(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n>=3 && n<=10 ? 3 : n>=11 && n<=99 ? 4 : 5)'),
39     # Assamese
40     # 'as': (),
41     # Avaric
42     # 'av': (),
43     # Aymara
44     # 'ay': (),
45     # Azerbaijani
46     # 'az': (),
47     # Bashkir
48     # 'ba': (),
49     # Belarusian
50     # 'be': (),
51     # Bulgarian - From Pootle's PO's
52     'bg': (2, '(n != 1)'),
53     # Bihari
54     # 'bh': (),
55     # Bislama
56     # 'bi': (),
57     # Bambara
58     # 'bm': (),
59     # Bengali - From Pootle's PO's
60     'bn': (2, '(n != 1)'),
61     # Tibetan - as discussed in private with Andrew West
62     'bo': (1, '0'),
63     # Breton
64     # 'br': (),
65     # Bosnian
66     # 'bs': (),
67     # Catalan - From Pootle's PO's
68     'ca': (2, '(n != 1)'),
69     # Chechen
70     # 'ce': (),
71     # Chamorro
72     # 'ch': (),
73     # Corsican
74     # 'co': (),
75     # Cree
76     # 'cr': (),
77     # Czech
78     'cs': (3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
79     # Church Slavic
80     # 'cu': (),
81     # Chuvash
82     'cv': (1, '0'),
83     # Welsh
84     'cy': (5, '(n==1 ? 1 : n==2 ? 2 : n==3 ? 3 : n==6 ? 4 : 0)'),
85     # Danish
86     'da': (2, '(n != 1)'),
87     # German
88     'de': (2, '(n != 1)'),
89     # Divehi
90     # 'dv': (),
91     # Dzongkha
92     'dz': (1, '0'),
93     # Greek
94     'el': (2, '(n != 1)'),
95     # English
96     'en': (2, '(n != 1)'),
97     # Esperanto
98     'eo': (2, '(n != 1)'),
99     # Spanish
100     'es': (2, '(n != 1)'),
101     # Estonian
102     'et': (2, '(n != 1)'),
103     # Basque - From Pootle's PO's
104     'eu': (2, '(n != 1)'),
105     # Persian - From Pootle's PO's
106     'fa': (1, '0'),
107     # Finnish
108     'fi': (2, '(n != 1)'),
109     # French
110     'fr': (2, '(n > 1)'),
111     # Friulian - From Pootle's PO's
112     'fur': (2, '(n > 1)'),
113     # Irish
114     'ga': (3, '(n==1 ? 0 : n==2 ? 1 : 2)'),
115     # Galician - From Pootle's PO's
116     'gl': (2, '(n != 1)'),
117     # Hausa - From Pootle's PO's
118     'ha': (2, '(n != 1)'),
119     # Hebrew
120     'he': (2, '(n != 1)'),
121     # Hindi - From Pootle's PO's
122     'hi': (2, '(n != 1)'),
123     # Croatian
124     'hr': (3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
125     # Hungarian
126     'hu': (1, '0'),
127     # Armenian - From Pootle's PO's
128     'hy': (1, '0'),
129     # Icelandic - From Pootle's PO's
130     'is': (2, '(n != 1)'),
131     # Italian
132     'it': (2, '(n != 1)'),
133     # Japanese
134     'ja': (1, '0'),
135     # Georgian - From Pootle's PO's
136     'ka': (1, '0'),
137     # Kongo - From Pootle's PO's
138     'kg': (2, '(n != 1)'),
139     # Khmer - From Pootle's PO's
140     'km': (1, '0'),
141     # Korean
142     'ko': (1, '0'),
143     # Kurdish - From Pootle's PO's
144     'ku': (2, '(n != 1)'),
145     # Lao - Another member of the Tai language family, like Thai.
146     'lo': (1, '0'),
147     # Lithuanian
148     'lt': (3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2)'),
149     # Latvian
150     'lv': (3, '(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2)'),
151     # Maltese - From Pootle's PO's
152     'mt': (4, '(n==1 ? 0 : n==0 || ( n%100>1 && n%100<11) ? 1 : (n%100>10 && n%100<20 ) ? 2 : 3)'),
153     # Norwegian BokmÃ¥l
154     'nb': (2, '(n != 1)'),
155     # Dutch
156     'nl': (2, '(n != 1)'),
157     # Norwegian Nynorsk
158     'nn': (2, '(n != 1)'),
159     # Norwegian
160     'no': (2, '(n != 1)'),
161     # Punjabi - From Pootle's PO's
162     'pa': (2, '(n != 1)'),
163     # Polish
164     'pl': (3, '(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
165     # Portuguese
166     'pt': (2, '(n != 1)'),
167     # Brazilian
168     'pt_BR': (2, '(n > 1)'),
169     # Romanian - From Pootle's PO's
170     'ro': (3, '(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2)'),
171     # Russian
172     'ru': (3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
173     # Slovak
174     'sk': (3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
175     # Slovenian
176     'sl': (4, '(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3)'),
177     # Serbian - From Pootle's PO's
178     'sr': (3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
179     # Southern Sotho - From Pootle's PO's
180     'st': (2, '(n != 1)'),
181     # Swedish
182     'sv': (2, '(n != 1)'),
183     # Thai
184     'th': (1, '0'),
185     # Turkish
186     'tr': (1, '0'),
187     # Ukrainian
188     'uk': (3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
189     # Venda - From Pootle's PO's
190     've': (2, '(n != 1)'),
191     # Vietnamese - From Pootle's PO's
192     'vi': (1, '0'),
193     # Xhosa - From Pootle's PO's
194     'xh': (2, '(n != 1)'),
195     # Chinese - From Pootle's PO's (modified)
196     'zh': (1, '0'),
197 }
198
199
200 DEFAULT_PLURAL = (2, '(n != 1)')
201
202
203 class _PluralTuple(tuple):
204     """A tuple with plural information."""
205
206     __slots__ = ()
207     num_plurals = property(itemgetter(0), doc="""
208     The number of plurals used by the locale.""")
209     plural_expr = property(itemgetter(1), doc="""
210     The plural expression used by the locale.""")
211     plural_forms = property(lambda x: 'nplurals=%s; plural=%s;' % x, doc="""
212     The plural expression used by the catalog or locale.""")
213
214     def __str__(self):
215         return self.plural_forms
216
217
218 def get_plural(locale=LC_CTYPE):
219     """A tuple with the information catalogs need to perform proper
220     pluralization.  The first item of the tuple is the number of plural
221     forms, the second the plural expression.
222
223     >>> get_plural(locale='en')
224     (2, '(n != 1)')
225     >>> get_plural(locale='ga')
226     (3, '(n==1 ? 0 : n==2 ? 1 : 2)')
227
228     The object returned is a special tuple with additional members:
229
230     >>> tup = get_plural("ja")
231     >>> tup.num_plurals
232     1
233     >>> tup.plural_expr
234     '0'
235     >>> tup.plural_forms
236     'nplurals=1; plural=0;'
237
238     Converting the tuple into a string prints the plural forms for a
239     gettext catalog:
240
241     >>> str(tup)
242     'nplurals=1; plural=0;'
243     """
244     locale = Locale.parse(locale)
245     try:
246         tup = PLURALS[str(locale)]
247     except KeyError:
248         try:
249             tup = PLURALS[locale.language]
250         except KeyError:
251             tup = DEFAULT_PLURAL
252     return _PluralTuple(tup)