Mercurial > babel > old > mirror
annotate babel/core.py @ 33:df1e2f0ef627
Raise error on unsupported locales. Closes #5.
author | cmlenz |
---|---|
date | Mon, 04 Jun 2007 11:29:55 +0000 |
parents | 9a00ac84004c |
children | 0505d666fa1f |
rev | line source |
---|---|
3 | 1 # -*- coding: utf-8 -*- |
2 # | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
3 # Copyright (C) 2007 Edgewall Software |
3 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
14 """Core locale representation and locale data access gateway.""" | |
15 | |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
16 from babel import localedata |
3 | 17 |
33 | 18 __all__ = ['UnknownLocaleError', 'Locale', 'negotiate', 'parse'] |
3 | 19 __docformat__ = 'restructuredtext en' |
20 | |
21 | |
33 | 22 class UnknownLocaleError(Exception): |
23 """Exception thrown when a locale is requested for which no locale data | |
24 is available. | |
25 """ | |
26 | |
27 def __init__(self, identifier): | |
28 """Create the exception. | |
29 | |
30 :param identifier: the identifier string of the unsupported locale | |
31 """ | |
32 Exception.__init__(self, 'unknown locale %r' % identifier) | |
33 self.identifier = identifier | |
34 | |
35 | |
3 | 36 class Locale(object): |
37 """Representation of a specific locale. | |
38 | |
39 >>> locale = Locale('en', territory='US') | |
40 >>> repr(locale) | |
41 '<Locale "en_US">' | |
42 >>> locale.display_name | |
43 u'English (United States)' | |
44 | |
45 A `Locale` object can also be instantiated from a raw locale string: | |
46 | |
47 >>> locale = Locale.parse('en-US', sep='-') | |
48 >>> repr(locale) | |
49 '<Locale "en_US">' | |
50 | |
51 `Locale` objects provide access to a collection of locale data, such as | |
52 territory and language names, number and date format patterns, and more: | |
53 | |
54 >>> locale.number_symbols['decimal'] | |
55 u'.' | |
33 | 56 |
57 If a locale is requested for which no locale data is available, an | |
58 `UnknownLocaleError` is raised: | |
59 | |
60 >>> Locale.parse('en_DE') | |
61 Traceback (most recent call last): | |
62 ... | |
63 UnknownLocaleError: unknown locale 'en_DE' | |
3 | 64 |
65 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_ | |
66 """ | |
67 | |
68 def __init__(self, language, territory=None, variant=None): | |
69 """Initialize the locale object from the given identifier components. | |
70 | |
71 >>> locale = Locale('en', 'US') | |
72 >>> locale.language | |
73 'en' | |
74 >>> locale.territory | |
75 'US' | |
76 | |
77 :param language: the language code | |
78 :param territory: the territory (country or region) code | |
79 :param variant: the variant code | |
33 | 80 :raise `UnknownLocaleError`: if no locale data is available for the |
81 requested locale | |
3 | 82 """ |
83 self.language = language | |
84 self.territory = territory | |
85 self.variant = variant | |
33 | 86 identifier = str(self) |
87 try: | |
88 self._data = localedata.load(identifier) | |
89 except IOError: | |
90 raise UnknownLocaleError(identifier) | |
3 | 91 |
92 def parse(cls, identifier, sep='_'): | |
93 """Create a `Locale` instance for the given locale identifier. | |
94 | |
95 >>> l = Locale.parse('de-DE', sep='-') | |
96 >>> l.display_name | |
97 u'Deutsch (Deutschland)' | |
98 | |
99 If the `identifier` parameter is not a string, but actually a `Locale` | |
100 object, that object is returned: | |
101 | |
102 >>> Locale.parse(l) | |
103 <Locale "de_DE"> | |
104 | |
105 :param identifier: the locale identifier string | |
106 :param sep: optional component separator | |
107 :return: a corresponding `Locale` instance | |
108 :rtype: `Locale` | |
109 :raise `ValueError`: if the string does not appear to be a valid locale | |
110 identifier | |
33 | 111 :raise `UnknownLocaleError`: if no locale data is available for the |
112 requested locale | |
3 | 113 """ |
114 if type(identifier) is cls: | |
115 return identifier | |
116 return cls(*parse(identifier, sep=sep)) | |
117 parse = classmethod(parse) | |
118 | |
119 def __repr__(self): | |
120 return '<Locale "%s">' % str(self) | |
121 | |
122 def __str__(self): | |
123 return '_'.join(filter(None, [self.language, self.territory, | |
124 self.variant])) | |
125 | |
126 def display_name(self): | |
127 retval = self.languages.get(self.language) | |
128 if self.territory: | |
129 variant = '' | |
130 if self.variant: | |
131 variant = ', %s' % self.variants.get(self.variant) | |
132 retval += ' (%s%s)' % (self.territories.get(self.territory), variant) | |
133 return retval | |
134 display_name = property(display_name, doc="""\ | |
135 The localized display name of the locale. | |
136 | |
137 >>> Locale('en').display_name | |
138 u'English' | |
139 >>> Locale('en', 'US').display_name | |
140 u'English (United States)' | |
141 | |
142 :type: `unicode` | |
143 """) | |
144 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
145 #{ General Locale Display Names |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
146 |
3 | 147 def languages(self): |
148 return self._data['languages'] | |
149 languages = property(languages, doc="""\ | |
150 Mapping of language codes to translated language names. | |
151 | |
152 >>> Locale('de', 'DE').languages['ja'] | |
153 u'Japanisch' | |
154 | |
155 :type: `dict` | |
156 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_ | |
157 """) | |
158 | |
159 def scripts(self): | |
160 return self._data['scripts'] | |
161 scripts = property(scripts, doc="""\ | |
162 Mapping of script codes to translated script names. | |
163 | |
164 >>> Locale('en', 'US').scripts['Hira'] | |
165 u'Hiragana' | |
166 | |
167 :type: `dict` | |
168 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_ | |
169 """) | |
170 | |
171 def territories(self): | |
172 return self._data['territories'] | |
173 territories = property(territories, doc="""\ | |
174 Mapping of script codes to translated script names. | |
175 | |
176 >>> Locale('es', 'CO').territories['DE'] | |
177 u'Alemania' | |
178 | |
179 :type: `dict` | |
180 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_ | |
181 """) | |
182 | |
183 def variants(self): | |
184 return self._data['variants'] | |
185 variants = property(variants, doc="""\ | |
186 Mapping of script codes to translated script names. | |
187 | |
188 >>> Locale('de', 'DE').variants['1901'] | |
189 u'alte deutsche Rechtschreibung' | |
190 | |
191 :type: `dict` | |
192 """) | |
193 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
194 #{ Number Formatting |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
195 |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
196 def currencies(self): |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
197 return self._data['currency_names'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
198 currencies = property(currencies, doc="""\ |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
199 Mapping of currency codes to translated currency names. |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
200 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
201 >>> Locale('en').currencies['COP'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
202 u'Colombian Peso' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
203 >>> Locale('de', 'DE').currencies['COP'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
204 u'Kolumbianischer Peso' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
205 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
206 :type: `dict` |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
207 """) |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
208 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
209 def currency_symbols(self): |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
210 return self._data['currency_symbols'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
211 currency_symbols = property(currency_symbols, doc="""\ |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
212 Mapping of currency codes to symbols. |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
213 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
214 >>> Locale('en').currency_symbols['USD'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
215 u'US$' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
216 >>> Locale('en', 'US').currency_symbols['USD'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
217 u'$' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
218 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
219 :type: `dict` |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
220 """) |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
221 |
3 | 222 def number_symbols(self): |
223 return self._data['number_symbols'] | |
224 number_symbols = property(number_symbols, doc="""\ | |
225 Symbols used in number formatting. | |
226 | |
227 >>> Locale('fr', 'FR').number_symbols['decimal'] | |
228 u',' | |
229 | |
230 :type: `dict` | |
231 """) | |
232 | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
233 def decimal_formats(self): |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
234 return self._data['decimal_formats'] |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
235 decimal_formats = property(decimal_formats, doc="""\ |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
236 Locale patterns for decimal number formatting. |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
237 |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
238 >>> Locale('en', 'US').decimal_formats[None] |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
239 <NumberPattern u'#,##0.###'> |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
240 |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
241 :type: `dict` |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
242 """) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
243 |
24 | 244 def percent_formats(self): |
245 return self._data['percent_formats'] | |
246 percent_formats = property(percent_formats, doc="""\ | |
247 Locale patterns for percent number formatting. | |
248 | |
249 >>> Locale('en', 'US').percent_formats[None] | |
250 <NumberPattern u'#,##0%'> | |
251 | |
252 :type: `dict` | |
253 """) | |
254 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
255 #{ Calendar Information and Date Formatting |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
256 |
3 | 257 def periods(self): |
258 return self._data['periods'] | |
259 periods = property(periods, doc="""\ | |
260 Locale display names for day periods (AM/PM). | |
261 | |
262 >>> Locale('en', 'US').periods['am'] | |
263 u'AM' | |
264 | |
265 :type: `dict` | |
266 """) | |
267 | |
268 def days(self): | |
269 return self._data['days'] | |
270 days = property(days, doc="""\ | |
271 Locale display names for weekdays. | |
272 | |
17 | 273 >>> Locale('de', 'DE').days['format']['wide'][3] |
3 | 274 u'Donnerstag' |
275 | |
276 :type: `dict` | |
277 """) | |
278 | |
279 def months(self): | |
280 return self._data['months'] | |
281 months = property(months, doc="""\ | |
282 Locale display names for months. | |
283 | |
284 >>> Locale('de', 'DE').months['format']['wide'][10] | |
285 u'Oktober' | |
286 | |
287 :type: `dict` | |
288 """) | |
289 | |
290 def quarters(self): | |
291 return self._data['quarters'] | |
292 quarters = property(quarters, doc="""\ | |
293 Locale display names for quarters. | |
294 | |
295 >>> Locale('de', 'DE').quarters['format']['wide'][1] | |
296 u'1. Quartal' | |
297 | |
298 :type: `dict` | |
299 """) | |
300 | |
301 def eras(self): | |
302 return self._data['eras'] | |
303 eras = property(eras, doc="""\ | |
304 Locale display names for eras. | |
305 | |
306 >>> Locale('en', 'US').eras['wide'][1] | |
307 u'Anno Domini' | |
308 >>> Locale('en', 'US').eras['abbreviated'][0] | |
309 u'BC' | |
310 | |
311 :type: `dict` | |
312 """) | |
313 | |
30
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
314 def time_zones(self): |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
315 return self._data['time_zones'] |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
316 time_zones = property(time_zones, doc="""\ |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
317 Locale display names for time zones. |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
318 |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
319 >>> Locale('en', 'US').time_zones['America/Los_Angeles']['long']['standard'] |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
320 u'Pacific Standard Time' |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
321 >>> Locale('en', 'US').time_zones['Europe/Dublin']['city'] |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
322 u'Dublin' |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
323 |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
324 :type: `dict` |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
325 """) |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
326 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
327 def first_week_day(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
328 return self._data['week_data']['first_day'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
329 first_week_day = property(first_week_day, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
330 The first day of a week. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
331 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
332 >>> Locale('de', 'DE').first_week_day |
17 | 333 0 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
334 >>> Locale('en', 'US').first_week_day |
17 | 335 6 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
336 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
337 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
338 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
339 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
340 def weekend_start(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
341 return self._data['week_data']['weekend_start'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
342 weekend_start = property(weekend_start, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
343 The day the weekend starts. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
344 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
345 >>> Locale('de', 'DE').weekend_start |
17 | 346 5 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
347 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
348 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
349 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
350 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
351 def weekend_end(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
352 return self._data['week_data']['weekend_end'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
353 weekend_end = property(weekend_end, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
354 The day the weekend ends. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
355 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
356 >>> Locale('de', 'DE').weekend_end |
17 | 357 6 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
358 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
359 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
360 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
361 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
362 def min_week_days(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
363 return self._data['week_data']['min_days'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
364 min_week_days = property(min_week_days, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
365 The minimum number of days in a week so that the week is counted as the |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
366 first week of a year or month. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
367 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
368 >>> Locale('de', 'DE').min_week_days |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
369 4 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
370 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
371 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
372 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
373 |
3 | 374 def date_formats(self): |
375 return self._data['date_formats'] | |
376 date_formats = property(date_formats, doc="""\ | |
377 Locale patterns for date formatting. | |
378 | |
379 >>> Locale('en', 'US').date_formats['short'] | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
380 <DateTimePattern u'M/d/yy'> |
3 | 381 >>> Locale('fr', 'FR').date_formats['long'] |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
382 <DateTimePattern u'd MMMM yyyy'> |
3 | 383 |
384 :type: `dict` | |
385 """) | |
386 | |
387 def time_formats(self): | |
388 return self._data['time_formats'] | |
389 time_formats = property(time_formats, doc="""\ | |
390 Locale patterns for time formatting. | |
391 | |
392 >>> Locale('en', 'US').time_formats['short'] | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
393 <DateTimePattern u'h:mm a'> |
3 | 394 >>> Locale('fr', 'FR').time_formats['long'] |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
395 <DateTimePattern u'HH:mm:ss z'> |
11 | 396 |
397 :type: `dict` | |
398 """) | |
399 | |
3 | 400 |
401 def negotiate(preferred, available): | |
402 """Find the best match between available and requested locale strings. | |
403 | |
404 >>> negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) | |
405 'de_DE' | |
406 >>> negotiate(['de_DE', 'en_US'], ['en', 'de']) | |
407 'de' | |
408 | |
409 :param preferred: the list of locale strings preferred by the user | |
410 :param available: the list of locale strings available | |
411 :return: the locale identifier for the best match, or `None` if no match | |
412 was found | |
413 :rtype: `str` | |
414 """ | |
415 for locale in preferred: | |
416 if locale in available: | |
417 return locale | |
418 parts = locale.split('_') | |
419 if len(parts) > 1 and parts[0] in available: | |
420 return parts[0] | |
421 return None | |
422 | |
423 def parse(identifier, sep='_'): | |
424 """Parse a locale identifier into a ``(language, territory, variant)`` | |
425 tuple. | |
426 | |
427 >>> parse('zh_CN') | |
428 ('zh', 'CN', None) | |
429 | |
430 The default component separator is "_", but a different separator can be | |
431 specified using the `sep` parameter: | |
432 | |
433 >>> parse('zh-CN', sep='-') | |
434 ('zh', 'CN', None) | |
435 | |
436 :param identifier: the locale identifier string | |
437 :param sep: character that separates the different parts of the locale | |
438 string | |
439 :return: the ``(language, territory, variant)`` tuple | |
440 :rtype: `tuple` | |
441 :raise `ValueError`: if the string does not appear to be a valid locale | |
442 identifier | |
443 | |
444 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_ | |
445 """ | |
446 parts = identifier.split(sep) | |
447 lang, territory, variant = parts[0].lower(), None, None | |
448 if not lang.isalpha(): | |
449 raise ValueError('expected only letters, got %r' % lang) | |
450 if len(parts) > 1: | |
451 territory = parts[1].upper().split('.', 1)[0] | |
452 if not territory.isalpha(): | |
453 raise ValueError('expected only letters, got %r' % territory) | |
454 if len(parts) > 2: | |
455 variant = parts[2].upper().split('.', 1)[0] | |
456 return lang, territory, variant |