Mercurial > babel > old > babel-test
annotate babel/core.py @ 41:e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
author | cmlenz |
---|---|
date | Wed, 06 Jun 2007 12:12:44 +0000 |
parents | 3b314a78015d |
children | 83f3f70c6ca3 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
3 # Copyright (C) 2007 Edgewall Software |
1 | 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 | |
39
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
16 import os |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
17 |
26
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
18 from babel import localedata |
1 | 19 |
39
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
20 __all__ = ['UnknownLocaleError', 'Locale', 'getdefault', 'negotiate', 'parse'] |
1 | 21 __docformat__ = 'restructuredtext en' |
22 | |
23 | |
31 | 24 class UnknownLocaleError(Exception): |
25 """Exception thrown when a locale is requested for which no locale data | |
26 is available. | |
27 """ | |
28 | |
29 def __init__(self, identifier): | |
30 """Create the exception. | |
31 | |
32 :param identifier: the identifier string of the unsupported locale | |
33 """ | |
34 Exception.__init__(self, 'unknown locale %r' % identifier) | |
35 self.identifier = identifier | |
36 | |
37 | |
1 | 38 class Locale(object): |
39 """Representation of a specific locale. | |
40 | |
41 >>> locale = Locale('en', territory='US') | |
42 >>> repr(locale) | |
43 '<Locale "en_US">' | |
44 >>> locale.display_name | |
45 u'English (United States)' | |
46 | |
47 A `Locale` object can also be instantiated from a raw locale string: | |
48 | |
49 >>> locale = Locale.parse('en-US', sep='-') | |
50 >>> repr(locale) | |
51 '<Locale "en_US">' | |
52 | |
53 `Locale` objects provide access to a collection of locale data, such as | |
54 territory and language names, number and date format patterns, and more: | |
55 | |
56 >>> locale.number_symbols['decimal'] | |
57 u'.' | |
31 | 58 |
59 If a locale is requested for which no locale data is available, an | |
60 `UnknownLocaleError` is raised: | |
61 | |
62 >>> Locale.parse('en_DE') | |
63 Traceback (most recent call last): | |
64 ... | |
65 UnknownLocaleError: unknown locale 'en_DE' | |
1 | 66 |
67 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_ | |
68 """ | |
69 | |
70 def __init__(self, language, territory=None, variant=None): | |
71 """Initialize the locale object from the given identifier components. | |
72 | |
73 >>> locale = Locale('en', 'US') | |
74 >>> locale.language | |
75 'en' | |
76 >>> locale.territory | |
77 'US' | |
78 | |
79 :param language: the language code | |
80 :param territory: the territory (country or region) code | |
81 :param variant: the variant code | |
31 | 82 :raise `UnknownLocaleError`: if no locale data is available for the |
83 requested locale | |
1 | 84 """ |
85 self.language = language | |
86 self.territory = territory | |
87 self.variant = variant | |
41
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
88 self.__data = None |
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
89 |
31 | 90 identifier = str(self) |
41
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
91 if not localedata.exists(identifier): |
31 | 92 raise UnknownLocaleError(identifier) |
1 | 93 |
39
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
94 def default(cls, category=None): |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
95 """Return the system default locale for the specified category. |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
96 |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
97 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']: |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
98 ... os.environ[name] = '' |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
99 >>> os.environ['LANG'] = 'fr_FR.UTF-8' |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
100 >>> Locale.default('LC_MESSAGES') |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
101 <Locale "fr_FR"> |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
102 |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
103 :param category: one of the ``LC_XXX`` environment variable names |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
104 :return: the value of the variable, or any of the fallbacks |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
105 (``LANGUAGE``, ``LC_ALL``, ``LC_CTYPE``, and ``LANG``) |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
106 :rtype: `Locale` |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
107 """ |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
108 return cls(getdefault(category)) |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
109 default = classmethod(default) |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
110 |
1 | 111 def parse(cls, identifier, sep='_'): |
112 """Create a `Locale` instance for the given locale identifier. | |
113 | |
114 >>> l = Locale.parse('de-DE', sep='-') | |
115 >>> l.display_name | |
116 u'Deutsch (Deutschland)' | |
117 | |
118 If the `identifier` parameter is not a string, but actually a `Locale` | |
119 object, that object is returned: | |
120 | |
121 >>> Locale.parse(l) | |
122 <Locale "de_DE"> | |
123 | |
124 :param identifier: the locale identifier string | |
125 :param sep: optional component separator | |
126 :return: a corresponding `Locale` instance | |
127 :rtype: `Locale` | |
128 :raise `ValueError`: if the string does not appear to be a valid locale | |
129 identifier | |
31 | 130 :raise `UnknownLocaleError`: if no locale data is available for the |
131 requested locale | |
1 | 132 """ |
133 if type(identifier) is cls: | |
134 return identifier | |
135 return cls(*parse(identifier, sep=sep)) | |
136 parse = classmethod(parse) | |
137 | |
138 def __repr__(self): | |
139 return '<Locale "%s">' % str(self) | |
140 | |
141 def __str__(self): | |
142 return '_'.join(filter(None, [self.language, self.territory, | |
143 self.variant])) | |
144 | |
41
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
145 def _data(self): |
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
146 if self.__data is None: |
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
147 self.__data = localedata.load(str(self)) |
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
148 return self.__data |
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
149 _data = property(_data) |
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
150 |
1 | 151 def display_name(self): |
152 retval = self.languages.get(self.language) | |
153 if self.territory: | |
154 variant = '' | |
155 if self.variant: | |
156 variant = ', %s' % self.variants.get(self.variant) | |
41
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
157 retval += ' (%s%s)' % (self.territories.get(self.territory), |
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
158 variant) |
1 | 159 return retval |
160 display_name = property(display_name, doc="""\ | |
161 The localized display name of the locale. | |
162 | |
163 >>> Locale('en').display_name | |
164 u'English' | |
165 >>> Locale('en', 'US').display_name | |
166 u'English (United States)' | |
41
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
167 >>> Locale('sv').display_name |
e967fbafcda1
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
39
diff
changeset
|
168 u'svenska' |
1 | 169 |
170 :type: `unicode` | |
171 """) | |
172 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
173 #{ General Locale Display Names |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
174 |
1 | 175 def languages(self): |
176 return self._data['languages'] | |
177 languages = property(languages, doc="""\ | |
178 Mapping of language codes to translated language names. | |
179 | |
180 >>> Locale('de', 'DE').languages['ja'] | |
181 u'Japanisch' | |
182 | |
183 :type: `dict` | |
184 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_ | |
185 """) | |
186 | |
187 def scripts(self): | |
188 return self._data['scripts'] | |
189 scripts = property(scripts, doc="""\ | |
190 Mapping of script codes to translated script names. | |
191 | |
192 >>> Locale('en', 'US').scripts['Hira'] | |
193 u'Hiragana' | |
194 | |
195 :type: `dict` | |
196 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_ | |
197 """) | |
198 | |
199 def territories(self): | |
200 return self._data['territories'] | |
201 territories = property(territories, doc="""\ | |
202 Mapping of script codes to translated script names. | |
203 | |
204 >>> Locale('es', 'CO').territories['DE'] | |
205 u'Alemania' | |
206 | |
207 :type: `dict` | |
208 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_ | |
209 """) | |
210 | |
211 def variants(self): | |
212 return self._data['variants'] | |
213 variants = property(variants, doc="""\ | |
214 Mapping of script codes to translated script names. | |
215 | |
216 >>> Locale('de', 'DE').variants['1901'] | |
217 u'alte deutsche Rechtschreibung' | |
218 | |
219 :type: `dict` | |
220 """) | |
221 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
222 #{ Number Formatting |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
223 |
26
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
224 def currencies(self): |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
225 return self._data['currency_names'] |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
226 currencies = property(currencies, doc="""\ |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
227 Mapping of currency codes to translated currency names. |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
228 |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
229 >>> Locale('en').currencies['COP'] |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
230 u'Colombian Peso' |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
231 >>> Locale('de', 'DE').currencies['COP'] |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
232 u'Kolumbianischer Peso' |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
233 |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
234 :type: `dict` |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
235 """) |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
236 |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
237 def currency_symbols(self): |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
238 return self._data['currency_symbols'] |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
239 currency_symbols = property(currency_symbols, doc="""\ |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
240 Mapping of currency codes to symbols. |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
241 |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
242 >>> Locale('en').currency_symbols['USD'] |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
243 u'US$' |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
244 >>> Locale('en', 'US').currency_symbols['USD'] |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
245 u'$' |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
246 |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
247 :type: `dict` |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
248 """) |
710090104678
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
22
diff
changeset
|
249 |
1 | 250 def number_symbols(self): |
251 return self._data['number_symbols'] | |
252 number_symbols = property(number_symbols, doc="""\ | |
253 Symbols used in number formatting. | |
254 | |
255 >>> Locale('fr', 'FR').number_symbols['decimal'] | |
256 u',' | |
257 | |
258 :type: `dict` | |
259 """) | |
260 | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
261 def decimal_formats(self): |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
262 return self._data['decimal_formats'] |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
263 decimal_formats = property(decimal_formats, doc="""\ |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
264 Locale patterns for decimal number formatting. |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
265 |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
266 >>> Locale('en', 'US').decimal_formats[None] |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
267 <NumberPattern u'#,##0.###'> |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
268 |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
269 :type: `dict` |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
270 """) |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
271 |
22 | 272 def percent_formats(self): |
273 return self._data['percent_formats'] | |
274 percent_formats = property(percent_formats, doc="""\ | |
275 Locale patterns for percent number formatting. | |
276 | |
277 >>> Locale('en', 'US').percent_formats[None] | |
278 <NumberPattern u'#,##0%'> | |
279 | |
280 :type: `dict` | |
281 """) | |
282 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
283 #{ Calendar Information and Date Formatting |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
284 |
1 | 285 def periods(self): |
286 return self._data['periods'] | |
287 periods = property(periods, doc="""\ | |
288 Locale display names for day periods (AM/PM). | |
289 | |
290 >>> Locale('en', 'US').periods['am'] | |
291 u'AM' | |
292 | |
293 :type: `dict` | |
294 """) | |
295 | |
296 def days(self): | |
297 return self._data['days'] | |
298 days = property(days, doc="""\ | |
299 Locale display names for weekdays. | |
300 | |
15 | 301 >>> Locale('de', 'DE').days['format']['wide'][3] |
1 | 302 u'Donnerstag' |
303 | |
304 :type: `dict` | |
305 """) | |
306 | |
307 def months(self): | |
308 return self._data['months'] | |
309 months = property(months, doc="""\ | |
310 Locale display names for months. | |
311 | |
312 >>> Locale('de', 'DE').months['format']['wide'][10] | |
313 u'Oktober' | |
314 | |
315 :type: `dict` | |
316 """) | |
317 | |
318 def quarters(self): | |
319 return self._data['quarters'] | |
320 quarters = property(quarters, doc="""\ | |
321 Locale display names for quarters. | |
322 | |
323 >>> Locale('de', 'DE').quarters['format']['wide'][1] | |
324 u'1. Quartal' | |
325 | |
326 :type: `dict` | |
327 """) | |
328 | |
329 def eras(self): | |
330 return self._data['eras'] | |
331 eras = property(eras, doc="""\ | |
332 Locale display names for eras. | |
333 | |
334 >>> Locale('en', 'US').eras['wide'][1] | |
335 u'Anno Domini' | |
336 >>> Locale('en', 'US').eras['abbreviated'][0] | |
337 u'BC' | |
338 | |
339 :type: `dict` | |
340 """) | |
341 | |
28
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
342 def time_zones(self): |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
343 return self._data['time_zones'] |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
344 time_zones = property(time_zones, doc="""\ |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
345 Locale display names for time zones. |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
346 |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
347 >>> Locale('en', 'US').time_zones['America/Los_Angeles']['long']['standard'] |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
348 u'Pacific Standard Time' |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
349 >>> Locale('en', 'US').time_zones['Europe/Dublin']['city'] |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
350 u'Dublin' |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
351 |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
352 :type: `dict` |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
353 """) |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
354 |
34 | 355 def zone_aliases(self): |
356 return self._data['zone_aliases'] | |
357 zone_aliases = property(zone_aliases, doc="""\ | |
358 Mapping of time zone aliases to their respective canonical identifer. | |
359 | |
360 >>> Locale('en').zone_aliases['UTC'] | |
361 'Etc/GMT' | |
362 | |
363 :type: `dict` | |
364 :note: this doesn't really belong here, as it does not change between | |
365 locales | |
366 """) | |
367 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
368 def first_week_day(self): |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
369 return self._data['week_data']['first_day'] |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
370 first_week_day = property(first_week_day, doc="""\ |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
371 The first day of a week. |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
372 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
373 >>> Locale('de', 'DE').first_week_day |
15 | 374 0 |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
375 >>> Locale('en', 'US').first_week_day |
15 | 376 6 |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
377 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
378 :type: `int` |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
379 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
380 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
381 def weekend_start(self): |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
382 return self._data['week_data']['weekend_start'] |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
383 weekend_start = property(weekend_start, doc="""\ |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
384 The day the weekend starts. |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
385 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
386 >>> Locale('de', 'DE').weekend_start |
15 | 387 5 |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
388 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
389 :type: `int` |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
390 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
391 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
392 def weekend_end(self): |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
393 return self._data['week_data']['weekend_end'] |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
394 weekend_end = property(weekend_end, doc="""\ |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
395 The day the weekend ends. |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
396 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
397 >>> Locale('de', 'DE').weekend_end |
15 | 398 6 |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
399 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
400 :type: `int` |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
401 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
402 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
403 def min_week_days(self): |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
404 return self._data['week_data']['min_days'] |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
405 min_week_days = property(min_week_days, doc="""\ |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
406 The minimum number of days in a week so that the week is counted as the |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
407 first week of a year or month. |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
408 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
409 >>> Locale('de', 'DE').min_week_days |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
410 4 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
411 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
412 :type: `int` |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
413 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
414 |
1 | 415 def date_formats(self): |
416 return self._data['date_formats'] | |
417 date_formats = property(date_formats, doc="""\ | |
418 Locale patterns for date formatting. | |
419 | |
420 >>> Locale('en', 'US').date_formats['short'] | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
421 <DateTimePattern u'M/d/yy'> |
1 | 422 >>> Locale('fr', 'FR').date_formats['long'] |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
423 <DateTimePattern u'd MMMM yyyy'> |
1 | 424 |
425 :type: `dict` | |
426 """) | |
427 | |
428 def time_formats(self): | |
429 return self._data['time_formats'] | |
430 time_formats = property(time_formats, doc="""\ | |
431 Locale patterns for time formatting. | |
432 | |
433 >>> Locale('en', 'US').time_formats['short'] | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
434 <DateTimePattern u'h:mm a'> |
1 | 435 >>> Locale('fr', 'FR').time_formats['long'] |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
436 <DateTimePattern u'HH:mm:ss z'> |
9 | 437 |
438 :type: `dict` | |
439 """) | |
440 | |
33 | 441 def datetime_formats(self): |
442 return self._data['datetime_formats'] | |
443 datetime_formats = property(datetime_formats, doc="""\ | |
444 Locale patterns for datetime formatting. | |
445 | |
446 >>> Locale('en').datetime_formats[None] | |
447 u'{1} {0}' | |
448 >>> Locale('th').datetime_formats[None] | |
449 u'{1}, {0}' | |
450 | |
451 :type: `dict` | |
452 """) | |
453 | |
1 | 454 |
39
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
455 def getdefault(category=None): |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
456 """Returns the system default locale for a given category, based on |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
457 environment variables. |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
458 |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
459 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']: |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
460 ... os.environ[name] = '' |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
461 >>> os.environ['LANG'] = 'fr_FR.UTF-8' |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
462 >>> getdefault('LC_MESSAGES') |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
463 'fr_FR' |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
464 |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
465 :param category: one of the ``LC_XXX`` environment variable names |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
466 :return: the value of the variable, or any of the fallbacks (``LANGUAGE``, |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
467 ``LC_ALL``, ``LC_CTYPE``, and ``LANG``) |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
468 |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
469 :rtype: `str` |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
470 """ |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
471 varnames = (category, 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG') |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
472 for name in filter(None, varnames): |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
473 locale = os.getenv(name) |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
474 if locale: |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
475 return '_'.join(filter(None, parse(locale))) |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
476 |
1 | 477 def negotiate(preferred, available): |
478 """Find the best match between available and requested locale strings. | |
479 | |
480 >>> negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) | |
481 'de_DE' | |
482 >>> negotiate(['de_DE', 'en_US'], ['en', 'de']) | |
483 'de' | |
484 | |
485 :param preferred: the list of locale strings preferred by the user | |
486 :param available: the list of locale strings available | |
487 :return: the locale identifier for the best match, or `None` if no match | |
488 was found | |
489 :rtype: `str` | |
490 """ | |
491 for locale in preferred: | |
492 if locale in available: | |
493 return locale | |
494 parts = locale.split('_') | |
495 if len(parts) > 1 and parts[0] in available: | |
496 return parts[0] | |
497 return None | |
498 | |
499 def parse(identifier, sep='_'): | |
500 """Parse a locale identifier into a ``(language, territory, variant)`` | |
501 tuple. | |
502 | |
503 >>> parse('zh_CN') | |
504 ('zh', 'CN', None) | |
505 | |
506 The default component separator is "_", but a different separator can be | |
507 specified using the `sep` parameter: | |
508 | |
509 >>> parse('zh-CN', sep='-') | |
510 ('zh', 'CN', None) | |
511 | |
512 :param identifier: the locale identifier string | |
513 :param sep: character that separates the different parts of the locale | |
514 string | |
515 :return: the ``(language, territory, variant)`` tuple | |
516 :rtype: `tuple` | |
517 :raise `ValueError`: if the string does not appear to be a valid locale | |
518 identifier | |
519 | |
520 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_ | |
521 """ | |
39
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
522 if '.' in identifier: |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
523 # this is probably the charset/encoding, which we don't care about |
3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
34
diff
changeset
|
524 identifier = identifier.split('.', 1)[0] |
1 | 525 parts = identifier.split(sep) |
526 lang, territory, variant = parts[0].lower(), None, None | |
527 if not lang.isalpha(): | |
528 raise ValueError('expected only letters, got %r' % lang) | |
529 if len(parts) > 1: | |
530 territory = parts[1].upper().split('.', 1)[0] | |
531 if not territory.isalpha(): | |
532 raise ValueError('expected only letters, got %r' % territory) | |
533 if len(parts) > 2: | |
534 variant = parts[2].upper().split('.', 1)[0] | |
535 return lang, territory, variant |