annotate babel/core.py @ 157:dbad98f3dbf6 stable-0.8.x

Ported [159] to 0.8.x branch.
author cmlenz
date Thu, 21 Jun 2007 11:43:36 +0000
parents f21c0725cdb4
children 0db5d8723c76
rev   line source
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
2 #
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
3 # Copyright (C) 2007 Edgewall Software
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
5 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
9 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
13
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
14 """Core locale representation and locale data access."""
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
19
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
20 __all__ = ['UnknownLocaleError', 'Locale', 'default_locale', 'negotiate_locale',
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
21 'parse_locale']
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
22 __docformat__ = 'restructuredtext en'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
23
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
24
31
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
25 class UnknownLocaleError(Exception):
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
26 """Exception thrown when a locale is requested for which no locale data
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
27 is available.
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
28 """
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
29
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
30 def __init__(self, identifier):
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
31 """Create the exception.
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
32
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
33 :param identifier: the identifier string of the unsupported locale
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
34 """
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
35 Exception.__init__(self, 'unknown locale %r' % identifier)
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
36 self.identifier = identifier
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
37
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
38
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
39 class Locale(object):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
40 """Representation of a specific locale.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
41
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
42 >>> locale = Locale('en', territory='US')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
43 >>> repr(locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
44 '<Locale "en_US">'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
45 >>> locale.display_name
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
46 u'English (United States)'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
47
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
48 A `Locale` object can also be instantiated from a raw locale string:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
49
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
50 >>> locale = Locale.parse('en-US', sep='-')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
51 >>> repr(locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
52 '<Locale "en_US">'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
53
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
54 `Locale` objects provide access to a collection of locale data, such as
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
55 territory and language names, number and date format patterns, and more:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
56
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
57 >>> locale.number_symbols['decimal']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
58 u'.'
31
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
59
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
60 If a locale is requested for which no locale data is available, an
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
61 `UnknownLocaleError` is raised:
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
62
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
63 >>> Locale.parse('en_DE')
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
64 Traceback (most recent call last):
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
65 ...
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
66 UnknownLocaleError: unknown locale 'en_DE'
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
67
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
68 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
69 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
70
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
71 def __init__(self, language, territory=None, variant=None):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
72 """Initialize the locale object from the given identifier components.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
73
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
74 >>> locale = Locale('en', 'US')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
75 >>> locale.language
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
76 'en'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
77 >>> locale.territory
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
78 'US'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
79
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
80 :param language: the language code
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
81 :param territory: the territory (country or region) code
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
82 :param variant: the variant code
31
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
83 :raise `UnknownLocaleError`: if no locale data is available for the
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
84 requested locale
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
85 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
86 self.language = language
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
87 self.territory = territory
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
88 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
89 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
90
31
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
91 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
92 if not localedata.exists(identifier):
31
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
93 raise UnknownLocaleError(identifier)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
94
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
95 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
96 """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
97
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 >>> 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
99 ... 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
100 >>> 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
101 >>> 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
102 <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
103
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 :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
105 :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
106 (``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
107 :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
108 """
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
109 return cls(default_locale(category))
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
110 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
111
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
112 def negotiate(cls, preferred, available, sep='_'):
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
113 """Find the best match between available and requested locale strings.
157
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
114
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
115 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
116 <Locale "de_DE">
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
117 >>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de'])
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
118 <Locale "de">
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
119 >>> Locale.negotiate(['de_DE', 'de'], ['en_US'])
157
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
120
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
121 You can specify the character used in the locale identifiers to separate
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
122 the differnet components. This separator is applied to both lists. Also,
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
123 case is ignored in the comparison:
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
124
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
125 >>> Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-')
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
126 <Locale "de_DE">
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
127
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
128 :param preferred: the list of locale identifers preferred by the user
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
129 :param available: the list of locale identifiers available
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
130 :return: the `Locale` object for the best match, or `None` if no match
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
131 was found
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
132 :rtype: `Locale`
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
133 """
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
134 identifier = negotiate_locale(preferred, available, sep=sep)
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
135 if identifier:
157
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
136 return Locale.parse(identifier, sep=sep)
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
137 negotiate = classmethod(negotiate)
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
138
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
139 def parse(cls, identifier, sep='_'):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
140 """Create a `Locale` instance for the given locale identifier.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
141
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
142 >>> l = Locale.parse('de-DE', sep='-')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
143 >>> l.display_name
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
144 u'Deutsch (Deutschland)'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
145
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
146 If the `identifier` parameter is not a string, but actually a `Locale`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
147 object, that object is returned:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
148
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
149 >>> Locale.parse(l)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
150 <Locale "de_DE">
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
151
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
152 :param identifier: the locale identifier string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
153 :param sep: optional component separator
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
154 :return: a corresponding `Locale` instance
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
155 :rtype: `Locale`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
156 :raise `ValueError`: if the string does not appear to be a valid locale
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
157 identifier
31
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
158 :raise `UnknownLocaleError`: if no locale data is available for the
b6ff3e4b43e5 Raise error on unsupported locales. Closes #5.
cmlenz
parents: 28
diff changeset
159 requested locale
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
160 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
161 if type(identifier) is cls:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
162 return identifier
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
163 return cls(*parse_locale(identifier, sep=sep))
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
164 parse = classmethod(parse)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
165
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
166 def __eq__(self, other):
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
167 return str(self) == str(other)
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
168
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
169 def __repr__(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
170 return '<Locale "%s">' % str(self)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
171
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
172 def __str__(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
173 return '_'.join(filter(None, [self.language, self.territory,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
174 self.variant]))
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
175
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
176 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
177 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
178 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
179 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
180 _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
181
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
182 def display_name(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
183 retval = self.languages.get(self.language)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
184 if self.territory:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
185 variant = ''
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
186 if self.variant:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
187 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
188 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
189 variant)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
190 return retval
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
191 display_name = property(display_name, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
192 The localized display name of the locale.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
193
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
194 >>> Locale('en').display_name
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
195 u'English'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
196 >>> Locale('en', 'US').display_name
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
197 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
198 >>> 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
199 u'svenska'
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
200
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
201 :type: `unicode`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
202 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
203
53
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
204 def english_name(self):
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
205 en = Locale('en')
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
206 retval = en.languages.get(self.language)
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
207 if self.territory:
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
208 variant = ''
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
209 if self.variant:
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
210 variant = ', %s' % en.variants.get(self.variant)
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
211 retval += ' (%s%s)' % (en.territories.get(self.territory),
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
212 variant)
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
213 return retval
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
214 english_name = property(english_name, doc="""\
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
215 The english display name of the locale.
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
216
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
217 >>> Locale('de').english_name
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
218 u'German'
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
219 >>> Locale('de', 'DE').english_name
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
220 u'German (Germany)'
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
221
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
222 :type: `unicode`
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
223 """)
83f3f70c6ca3 Add `english_name` property to `Locale` class.
cmlenz
parents: 41
diff changeset
224
8
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
225 #{ 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
226
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
227 def languages(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
228 return self._data['languages']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
229 languages = property(languages, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
230 Mapping of language codes to translated language names.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
231
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
232 >>> Locale('de', 'DE').languages['ja']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
233 u'Japanisch'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
234
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
235 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
236 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
237 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
238
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
239 def scripts(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
240 return self._data['scripts']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
241 scripts = property(scripts, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
242 Mapping of script codes to translated script names.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
243
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
244 >>> Locale('en', 'US').scripts['Hira']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
245 u'Hiragana'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
246
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
247 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
248 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
249 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
250
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
251 def territories(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
252 return self._data['territories']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
253 territories = property(territories, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
254 Mapping of script codes to translated script names.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
255
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
256 >>> Locale('es', 'CO').territories['DE']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
257 u'Alemania'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
258
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
259 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
260 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
261 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
262
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
263 def variants(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
264 return self._data['variants']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
265 variants = property(variants, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
266 Mapping of script codes to translated script names.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
267
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
268 >>> Locale('de', 'DE').variants['1901']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
269 u'alte deutsche Rechtschreibung'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
270
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
271 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
272 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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 #{ 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
275
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
276 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
277 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
278 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
279 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
280
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
281 >>> 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
282 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
283 >>> 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
284 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
285
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
286 :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
287 """)
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
288
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
289 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
290 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
291 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
292 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
293
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
294 >>> 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
295 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
296 >>> 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
297 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
298
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
299 :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
300 """)
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
301
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
302 def number_symbols(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
303 return self._data['number_symbols']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
304 number_symbols = property(number_symbols, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
305 Symbols used in number formatting.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
306
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
307 >>> Locale('fr', 'FR').number_symbols['decimal']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
308 u','
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
309
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
310 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
311 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
312
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
313 def decimal_formats(self):
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
314 return self._data['decimal_formats']
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
315 decimal_formats = property(decimal_formats, doc="""\
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
316 Locale patterns for decimal number formatting.
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
317
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
318 >>> Locale('en', 'US').decimal_formats[None]
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
319 <NumberPattern u'#,##0.###'>
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
320
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
321 :type: `dict`
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
322 """)
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
323
125
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
324 def currency_formats(self):
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
325 return self._data['currency_formats']
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
326 currency_formats = property(currency_formats, doc=r"""\
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
327 Locale patterns for currency number formatting.
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
328
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
329 >>> print Locale('en', 'US').currency_formats[None]
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
330 <NumberPattern u'\xa4#,##0.00'>
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
331
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
332 :type: `dict`
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
333 """)
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
334
22
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
335 def percent_formats(self):
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
336 return self._data['percent_formats']
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
337 percent_formats = property(percent_formats, doc="""\
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
338 Locale patterns for percent number formatting.
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
339
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
340 >>> Locale('en', 'US').percent_formats[None]
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
341 <NumberPattern u'#,##0%'>
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
342
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
343 :type: `dict`
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
344 """)
7d37639a7411 Implemented babel.numbers.format_percent
jonas
parents: 15
diff changeset
345
125
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
346 def scientific_formats(self):
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
347 return self._data['scientific_formats']
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
348 scientific_formats = property(scientific_formats, doc="""\
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
349 Locale patterns for scientific number formatting.
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
350
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
351 >>> Locale('en', 'US').scientific_formats[None]
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
352 <NumberPattern u'#E0'>
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
353
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
354 :type: `dict`
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
355 """)
b75ae5def3b1 Add currency formatting.
cmlenz
parents: 72
diff changeset
356
8
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
357 #{ 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
358
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
359 def periods(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
360 return self._data['periods']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
361 periods = property(periods, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
362 Locale display names for day periods (AM/PM).
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
363
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
364 >>> Locale('en', 'US').periods['am']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
365 u'AM'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
366
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
367 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
368 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
369
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
370 def days(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
371 return self._data['days']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
372 days = property(days, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
373 Locale display names for weekdays.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
374
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
375 >>> Locale('de', 'DE').days['format']['wide'][3]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
376 u'Donnerstag'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
377
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
378 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
379 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
380
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
381 def months(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
382 return self._data['months']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
383 months = property(months, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
384 Locale display names for months.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
385
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
386 >>> Locale('de', 'DE').months['format']['wide'][10]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
387 u'Oktober'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
388
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
389 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
390 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
391
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
392 def quarters(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
393 return self._data['quarters']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
394 quarters = property(quarters, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
395 Locale display names for quarters.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
396
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
397 >>> Locale('de', 'DE').quarters['format']['wide'][1]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
398 u'1. Quartal'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
399
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
400 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
401 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
402
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
403 def eras(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
404 return self._data['eras']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
405 eras = property(eras, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
406 Locale display names for eras.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
407
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
408 >>> Locale('en', 'US').eras['wide'][1]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
409 u'Anno Domini'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
410 >>> Locale('en', 'US').eras['abbreviated'][0]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
411 u'BC'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
412
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
413 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
414 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
415
28
11278622ede9 Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents: 26
diff changeset
416 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
417 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
418 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
419 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
420
11278622ede9 Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents: 26
diff changeset
421 >>> 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
422 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
423 >>> 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
424 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
425
11278622ede9 Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents: 26
diff changeset
426 :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
427 """)
11278622ede9 Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents: 26
diff changeset
428
34
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
429 def zone_aliases(self):
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
430 return self._data['zone_aliases']
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
431 zone_aliases = property(zone_aliases, doc="""\
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
432 Mapping of time zone aliases to their respective canonical identifer.
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
433
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
434 >>> Locale('en').zone_aliases['UTC']
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
435 'Etc/GMT'
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
436
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
437 :type: `dict`
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
438 :note: this doesn't really belong here, as it does not change between
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
439 locales
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
440 """)
3666f3d3df15 Extended time-zone support.
cmlenz
parents: 33
diff changeset
441
8
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
442 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
443 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
444 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
445 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
446
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
447 >>> Locale('de', 'DE').first_week_day
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
448 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
449 >>> Locale('en', 'US').first_week_day
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
450 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
451
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
452 :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
453 """)
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
454
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
455 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
456 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
457 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
458 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
459
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
460 >>> Locale('de', 'DE').weekend_start
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
461 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
462
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
463 :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
464 """)
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
465
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
466 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
467 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
468 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
469 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
470
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
471 >>> Locale('de', 'DE').weekend_end
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
472 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
473
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
474 :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
475 """)
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
476
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
477 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
478 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
479 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
480 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
481 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
482
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
483 >>> 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
484 4
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
485
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
486 :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
487 """)
9132c9218745 Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 1
diff changeset
488
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
489 def date_formats(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
490 return self._data['date_formats']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
491 date_formats = property(date_formats, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
492 Locale patterns for date formatting.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
493
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
494 >>> Locale('en', 'US').date_formats['short']
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
495 <DateTimePattern u'M/d/yy'>
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
496 >>> Locale('fr', 'FR').date_formats['long']
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
497 <DateTimePattern u'd MMMM yyyy'>
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
498
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
499 :type: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
500 """)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
501
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
502 def time_formats(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
503 return self._data['time_formats']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
504 time_formats = property(time_formats, doc="""\
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
505 Locale patterns for time formatting.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
506
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
507 >>> Locale('en', 'US').time_formats['short']
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
508 <DateTimePattern u'h:mm a'>
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
509 >>> Locale('fr', 'FR').time_formats['long']
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 9
diff changeset
510 <DateTimePattern u'HH:mm:ss z'>
9
3be73c6f01f1 Add basic support for number format patterns.
jonas
parents: 8
diff changeset
511
3be73c6f01f1 Add basic support for number format patterns.
jonas
parents: 8
diff changeset
512 :type: `dict`
3be73c6f01f1 Add basic support for number format patterns.
jonas
parents: 8
diff changeset
513 """)
3be73c6f01f1 Add basic support for number format patterns.
jonas
parents: 8
diff changeset
514
33
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
515 def datetime_formats(self):
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
516 return self._data['datetime_formats']
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
517 datetime_formats = property(datetime_formats, doc="""\
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
518 Locale patterns for datetime formatting.
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
519
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
520 >>> Locale('en').datetime_formats[None]
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
521 u'{1} {0}'
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
522 >>> Locale('th').datetime_formats[None]
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
523 u'{1}, {0}'
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
524
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
525 :type: `dict`
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
526 """)
0740b6d31799 * Import datetime patterns from CLDR.
cmlenz
parents: 31
diff changeset
527
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
528
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
529 def default_locale(category=None):
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
530 """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
531 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
532
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
533 >>> 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
534 ... 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
535 >>> os.environ['LANG'] = 'fr_FR.UTF-8'
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
536 >>> default_locale('LC_MESSAGES')
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
537 '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
538
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
539 :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
540 :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
541 ``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
542
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
543 :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
544 """
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
545 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
546 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
547 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
548 if locale:
151
f21c0725cdb4 Merged [141:143] and [145:153] via svnmerge from [source:/trunk].
cmlenz
parents: 125
diff changeset
549 if name == 'LANGUAGE' and ':' in locale:
f21c0725cdb4 Merged [141:143] and [145:153] via svnmerge from [source:/trunk].
cmlenz
parents: 125
diff changeset
550 # the LANGUAGE variable may contain a colon-separated list of
f21c0725cdb4 Merged [141:143] and [145:153] via svnmerge from [source:/trunk].
cmlenz
parents: 125
diff changeset
551 # language codes; we just pick the language on the list
f21c0725cdb4 Merged [141:143] and [145:153] via svnmerge from [source:/trunk].
cmlenz
parents: 125
diff changeset
552 locale = locale.split(':')[0]
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
553 return '_'.join(filter(None, parse_locale(locale)))
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
554
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
555 def negotiate_locale(preferred, available, sep='_'):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
556 """Find the best match between available and requested locale strings.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
557
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
558 >>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
559 'de_DE'
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
560 >>> negotiate_locale(['de_DE', 'en_US'], ['en', 'de'])
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
561 'de'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
562
157
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
563 Case is ignored by the algorithm, the result uses the case of the preferred
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
564 locale identifier:
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
565
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
566 >>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at'])
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
567 'de_DE'
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
568
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
569 :param preferred: the list of locale strings preferred by the user
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
570 :param available: the list of locale strings available
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
571 :param sep: character that separates the different parts of the locale
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
572 strings
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
573 :return: the locale identifier for the best match, or `None` if no match
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
574 was found
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
575 :rtype: `str`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
576 """
157
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
577 available = [a.lower() for a in available if a]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
578 for locale in preferred:
157
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
579 if locale.lower() in available:
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
580 return locale
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
581 parts = locale.split(sep)
157
dbad98f3dbf6 Ported [159] to 0.8.x branch.
cmlenz
parents: 151
diff changeset
582 if len(parts) > 1 and parts[0].lower() in available:
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
583 return parts[0]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
584 return None
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
585
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
586 def parse_locale(identifier, sep='_'):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
587 """Parse a locale identifier into a ``(language, territory, variant)``
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
588 tuple.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
589
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
590 >>> parse_locale('zh_CN')
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
591 ('zh', 'CN', None)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
592
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
593 The default component separator is "_", but a different separator can be
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
594 specified using the `sep` parameter:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
595
72
4dcdb1d367ec More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents: 53
diff changeset
596 >>> parse_locale('zh-CN', sep='-')
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
597 ('zh', 'CN', None)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
598
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
599 :param identifier: the locale identifier string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
600 :param sep: character that separates the different parts of the locale
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
601 string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
602 :return: the ``(language, territory, variant)`` tuple
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
603 :rtype: `tuple`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
604 :raise `ValueError`: if the string does not appear to be a valid locale
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
605 identifier
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
606
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
607 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
608 """
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
609 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
610 # 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
611 identifier = identifier.split('.', 1)[0]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
612 parts = identifier.split(sep)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
613 lang, territory, variant = parts[0].lower(), None, None
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
614 if not lang.isalpha():
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
615 raise ValueError('expected only letters, got %r' % lang)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
616 if len(parts) > 1:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
617 territory = parts[1].upper().split('.', 1)[0]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
618 if not territory.isalpha():
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
619 raise ValueError('expected only letters, got %r' % territory)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
620 if len(parts) > 2:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
621 variant = parts[2].upper().split('.', 1)[0]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
622 return lang, territory, variant
Copyright (C) 2012-2017 Edgewall Software