Mercurial > babel > old > babel-test
annotate babel/core.py @ 39:3b314a78015d
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
author | cmlenz |
---|---|
date | Wed, 06 Jun 2007 11:02:24 +0000 |
parents | 3666f3d3df15 |
children | e967fbafcda1 |
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 | |
31 | 88 identifier = str(self) |
89 try: | |
90 self._data = localedata.load(identifier) | |
91 except IOError: | |
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 | |
145 def display_name(self): | |
146 retval = self.languages.get(self.language) | |
147 if self.territory: | |
148 variant = '' | |
149 if self.variant: | |
150 variant = ', %s' % self.variants.get(self.variant) | |
151 retval += ' (%s%s)' % (self.territories.get(self.territory), variant) | |
152 return retval | |
153 display_name = property(display_name, doc="""\ | |
154 The localized display name of the locale. | |
155 | |
156 >>> Locale('en').display_name | |
157 u'English' | |
158 >>> Locale('en', 'US').display_name | |
159 u'English (United States)' | |
160 | |
161 :type: `unicode` | |
162 """) | |
163 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
164 #{ 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
|
165 |
1 | 166 def languages(self): |
167 return self._data['languages'] | |
168 languages = property(languages, doc="""\ | |
169 Mapping of language codes to translated language names. | |
170 | |
171 >>> Locale('de', 'DE').languages['ja'] | |
172 u'Japanisch' | |
173 | |
174 :type: `dict` | |
175 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_ | |
176 """) | |
177 | |
178 def scripts(self): | |
179 return self._data['scripts'] | |
180 scripts = property(scripts, doc="""\ | |
181 Mapping of script codes to translated script names. | |
182 | |
183 >>> Locale('en', 'US').scripts['Hira'] | |
184 u'Hiragana' | |
185 | |
186 :type: `dict` | |
187 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_ | |
188 """) | |
189 | |
190 def territories(self): | |
191 return self._data['territories'] | |
192 territories = property(territories, doc="""\ | |
193 Mapping of script codes to translated script names. | |
194 | |
195 >>> Locale('es', 'CO').territories['DE'] | |
196 u'Alemania' | |
197 | |
198 :type: `dict` | |
199 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_ | |
200 """) | |
201 | |
202 def variants(self): | |
203 return self._data['variants'] | |
204 variants = property(variants, doc="""\ | |
205 Mapping of script codes to translated script names. | |
206 | |
207 >>> Locale('de', 'DE').variants['1901'] | |
208 u'alte deutsche Rechtschreibung' | |
209 | |
210 :type: `dict` | |
211 """) | |
212 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
213 #{ 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
|
214 |
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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 |
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
|
220 >>> 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
|
221 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
|
222 >>> 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
|
223 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
|
224 |
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 :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
|
226 """) |
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 |
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 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
|
229 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
|
230 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
|
231 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
|
232 |
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 >>> 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
|
234 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
|
235 >>> 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
|
236 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
|
237 |
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 :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
|
239 """) |
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 |
1 | 241 def number_symbols(self): |
242 return self._data['number_symbols'] | |
243 number_symbols = property(number_symbols, doc="""\ | |
244 Symbols used in number formatting. | |
245 | |
246 >>> Locale('fr', 'FR').number_symbols['decimal'] | |
247 u',' | |
248 | |
249 :type: `dict` | |
250 """) | |
251 | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
252 def decimal_formats(self): |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
253 return self._data['decimal_formats'] |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
254 decimal_formats = property(decimal_formats, doc="""\ |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
255 Locale patterns for decimal number formatting. |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
256 |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
257 >>> Locale('en', 'US').decimal_formats[None] |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
258 <NumberPattern u'#,##0.###'> |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
259 |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
260 :type: `dict` |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
261 """) |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
262 |
22 | 263 def percent_formats(self): |
264 return self._data['percent_formats'] | |
265 percent_formats = property(percent_formats, doc="""\ | |
266 Locale patterns for percent number formatting. | |
267 | |
268 >>> Locale('en', 'US').percent_formats[None] | |
269 <NumberPattern u'#,##0%'> | |
270 | |
271 :type: `dict` | |
272 """) | |
273 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
274 #{ 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
|
275 |
1 | 276 def periods(self): |
277 return self._data['periods'] | |
278 periods = property(periods, doc="""\ | |
279 Locale display names for day periods (AM/PM). | |
280 | |
281 >>> Locale('en', 'US').periods['am'] | |
282 u'AM' | |
283 | |
284 :type: `dict` | |
285 """) | |
286 | |
287 def days(self): | |
288 return self._data['days'] | |
289 days = property(days, doc="""\ | |
290 Locale display names for weekdays. | |
291 | |
15 | 292 >>> Locale('de', 'DE').days['format']['wide'][3] |
1 | 293 u'Donnerstag' |
294 | |
295 :type: `dict` | |
296 """) | |
297 | |
298 def months(self): | |
299 return self._data['months'] | |
300 months = property(months, doc="""\ | |
301 Locale display names for months. | |
302 | |
303 >>> Locale('de', 'DE').months['format']['wide'][10] | |
304 u'Oktober' | |
305 | |
306 :type: `dict` | |
307 """) | |
308 | |
309 def quarters(self): | |
310 return self._data['quarters'] | |
311 quarters = property(quarters, doc="""\ | |
312 Locale display names for quarters. | |
313 | |
314 >>> Locale('de', 'DE').quarters['format']['wide'][1] | |
315 u'1. Quartal' | |
316 | |
317 :type: `dict` | |
318 """) | |
319 | |
320 def eras(self): | |
321 return self._data['eras'] | |
322 eras = property(eras, doc="""\ | |
323 Locale display names for eras. | |
324 | |
325 >>> Locale('en', 'US').eras['wide'][1] | |
326 u'Anno Domini' | |
327 >>> Locale('en', 'US').eras['abbreviated'][0] | |
328 u'BC' | |
329 | |
330 :type: `dict` | |
331 """) | |
332 | |
28
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
333 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
|
334 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
|
335 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
|
336 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
|
337 |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
338 >>> 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
|
339 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
|
340 >>> 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
|
341 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
|
342 |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
343 :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
|
344 """) |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
345 |
34 | 346 def zone_aliases(self): |
347 return self._data['zone_aliases'] | |
348 zone_aliases = property(zone_aliases, doc="""\ | |
349 Mapping of time zone aliases to their respective canonical identifer. | |
350 | |
351 >>> Locale('en').zone_aliases['UTC'] | |
352 'Etc/GMT' | |
353 | |
354 :type: `dict` | |
355 :note: this doesn't really belong here, as it does not change between | |
356 locales | |
357 """) | |
358 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
359 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
|
360 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
|
361 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
|
362 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
|
363 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
364 >>> Locale('de', 'DE').first_week_day |
15 | 365 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
|
366 >>> Locale('en', 'US').first_week_day |
15 | 367 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
|
368 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
369 :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
|
370 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
371 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
372 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
|
373 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
|
374 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
|
375 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
|
376 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
377 >>> Locale('de', 'DE').weekend_start |
15 | 378 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
|
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 :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
|
381 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
382 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
383 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
|
384 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
|
385 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
|
386 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
|
387 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
388 >>> Locale('de', 'DE').weekend_end |
15 | 389 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
|
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 :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
|
392 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
393 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
394 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
|
395 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
|
396 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
|
397 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
|
398 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
|
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 >>> 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
|
401 4 |
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 :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
|
404 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
405 |
1 | 406 def date_formats(self): |
407 return self._data['date_formats'] | |
408 date_formats = property(date_formats, doc="""\ | |
409 Locale patterns for date formatting. | |
410 | |
411 >>> Locale('en', 'US').date_formats['short'] | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
412 <DateTimePattern u'M/d/yy'> |
1 | 413 >>> Locale('fr', 'FR').date_formats['long'] |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
414 <DateTimePattern u'd MMMM yyyy'> |
1 | 415 |
416 :type: `dict` | |
417 """) | |
418 | |
419 def time_formats(self): | |
420 return self._data['time_formats'] | |
421 time_formats = property(time_formats, doc="""\ | |
422 Locale patterns for time formatting. | |
423 | |
424 >>> Locale('en', 'US').time_formats['short'] | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
425 <DateTimePattern u'h:mm a'> |
1 | 426 >>> Locale('fr', 'FR').time_formats['long'] |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
427 <DateTimePattern u'HH:mm:ss z'> |
9 | 428 |
429 :type: `dict` | |
430 """) | |
431 | |
33 | 432 def datetime_formats(self): |
433 return self._data['datetime_formats'] | |
434 datetime_formats = property(datetime_formats, doc="""\ | |
435 Locale patterns for datetime formatting. | |
436 | |
437 >>> Locale('en').datetime_formats[None] | |
438 u'{1} {0}' | |
439 >>> Locale('th').datetime_formats[None] | |
440 u'{1}, {0}' | |
441 | |
442 :type: `dict` | |
443 """) | |
444 | |
1 | 445 |
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
|
446 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
|
447 """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
|
448 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
|
449 |
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
|
450 >>> 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
|
451 ... 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
|
452 >>> 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
|
453 >>> 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
|
454 '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
|
455 |
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 :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
|
457 :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
|
458 ``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
|
459 |
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 :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
|
461 """ |
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 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
|
463 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
|
464 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
|
465 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
|
466 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
|
467 |
1 | 468 def negotiate(preferred, available): |
469 """Find the best match between available and requested locale strings. | |
470 | |
471 >>> negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) | |
472 'de_DE' | |
473 >>> negotiate(['de_DE', 'en_US'], ['en', 'de']) | |
474 'de' | |
475 | |
476 :param preferred: the list of locale strings preferred by the user | |
477 :param available: the list of locale strings available | |
478 :return: the locale identifier for the best match, or `None` if no match | |
479 was found | |
480 :rtype: `str` | |
481 """ | |
482 for locale in preferred: | |
483 if locale in available: | |
484 return locale | |
485 parts = locale.split('_') | |
486 if len(parts) > 1 and parts[0] in available: | |
487 return parts[0] | |
488 return None | |
489 | |
490 def parse(identifier, sep='_'): | |
491 """Parse a locale identifier into a ``(language, territory, variant)`` | |
492 tuple. | |
493 | |
494 >>> parse('zh_CN') | |
495 ('zh', 'CN', None) | |
496 | |
497 The default component separator is "_", but a different separator can be | |
498 specified using the `sep` parameter: | |
499 | |
500 >>> parse('zh-CN', sep='-') | |
501 ('zh', 'CN', None) | |
502 | |
503 :param identifier: the locale identifier string | |
504 :param sep: character that separates the different parts of the locale | |
505 string | |
506 :return: the ``(language, territory, variant)`` tuple | |
507 :rtype: `tuple` | |
508 :raise `ValueError`: if the string does not appear to be a valid locale | |
509 identifier | |
510 | |
511 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_ | |
512 """ | |
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
|
513 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
|
514 # 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
|
515 identifier = identifier.split('.', 1)[0] |
1 | 516 parts = identifier.split(sep) |
517 lang, territory, variant = parts[0].lower(), None, None | |
518 if not lang.isalpha(): | |
519 raise ValueError('expected only letters, got %r' % lang) | |
520 if len(parts) > 1: | |
521 territory = parts[1].upper().split('.', 1)[0] | |
522 if not territory.isalpha(): | |
523 raise ValueError('expected only letters, got %r' % territory) | |
524 if len(parts) > 2: | |
525 variant = parts[2].upper().split('.', 1)[0] | |
526 return lang, territory, variant |